Skip to content

Throw an exception for suspending factory methods #32719

@Hansanto

Description

@Hansanto

Hello

The @Bean seems to be called two times according to logs (below) in a suspend kotlin function.

This is my app code:

@SpringBootApplication
class SpringKaultApplication {

    @Bean
    suspend fun greeting(): String {
        println("Hello, World!")
        return "My test"
    }
}

fun main(args: Array<String>) {
    runApplication<SpringKaultApplication>(*args)
}

And the logs when I started the application:

2024-04-27T18:14:29.228+00:00  INFO 27824 --- [           main] o.e.s.SpringApplicationKt           : Starting SpringApplicationKt using Java 19.0.2
2024-04-27T18:14:29.230+00:00  INFO 27824 --- [           main] o.e.s.SpringApplicationKt           : No active profile set, falling back to 1 default profile: "default"
Hello, World!
Hello, World!
2024-04-27T18:14:30.566+00:00  INFO 27824 --- [           main] o.s.b.web.embedded.netty.NettyWebServer  : Netty started on port 8080
2024-04-27T18:14:30.572+00:00  INFO 27824 --- [           main] o.e.s.SpringApplicationKt           : Started SpringApplicationKt in 1.663 seconds (process running for 2.204)

Now, if I try without the suspend keyword like:

@SpringBootApplication
class SpringApplication {

    @Bean
    fun greeting(): String {
        println("Hello, World!")
        return "My test"
    }
}

fun main(args: Array<String>) {
    runApplication<SpringApplication>(*args)
}

And the logs:

2024-04-27T18:21:41.495+00:00  INFO 27660 --- [           main] o.e.s.SpringApplicationKt        : Starting SpringApplicationKt using Java 19.0.2
2024-04-27T18:21:41.497+00:00  INFO 27660 --- [           main] o.e.s.SpringApplicationKt        : No active profile set, falling back to 1 default profile: "default"
Hello, World!
2024-04-27T18:21:42.936+00:00  INFO 27660 --- [           main] o.s.b.web.embedded.netty.NettyWebServer  : Netty started on port 8080
2024-04-27T18:21:42.942+00:00  INFO 27660 --- [           main] o.e.s.SpringApplicationKt        : Started SpringApplicationKt in 1.755 seconds (process running for 2.226)

There are the other file to reproduces:

build.gradle.kts

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    id("org.springframework.boot") version "3.2.5"
    id("io.spring.dependency-management") version "1.1.4"
    kotlin("jvm") version "1.9.23"
    kotlin("plugin.spring") version "1.9.23"
}

group = "org.example"
version = "0.0.1-SNAPSHOT"

java {
    sourceCompatibility = JavaVersion.VERSION_17
}

repositories {
    mavenCentral()
}

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-webflux")
    implementation("io.projectreactor.kotlin:reactor-kotlin-extensions")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor")
    testImplementation("org.springframework.boot:spring-boot-starter-test")
    testImplementation("io.projectreactor:reactor-test")
}

tasks.withType<KotlinCompile> {
    kotlinOptions {
        freeCompilerArgs += "-Xjsr305=strict"
        jvmTarget = "17"
    }
}

tasks.withType<Test> {
    useJUnitPlatform()
}

Version:

  • Java: 19.0.2
  • Kotlin: 1.9.23
  • Spring boot: 3.2.5
  • Gradle: 8.7

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)theme: kotlinAn issue related to Kotlin supporttype: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions