Like the coroutines repository added in the Spring Data and Coroutines Router DSL in the Spring framework. Add Coroutines support for ApplicationRunner and CommandLineRuner to accept suspend functions.

Currently I have to use a runBlocking to wrap my Kotlin Coroutines codes.

@Component
class SampleDataInitializer(val posts: PostRepository) : ApplicationRunner {
    override fun run(args: ApplicationArguments?) {
        runBlocking {
            posts.save(Post(title = "Sample Post", content = "Sample Content"))
        }
    }

}

// here PostRepository extends CoroutineCrudRepository

Hope simply remove the runBlocking{} like this.

@Component
class SampleDataInitializer(val posts: PostRepository) : CouroutineApplicationRunner {
    override suspend fun run(args: ApplicationArguments?) {
        posts.save(Post(title = "Sample Post", content = "Sample Content"))
    }
}

Comment From: wilkinsona

Thanks for the suggestion. Can you please provide some examples of when this support would be useful to you?

Comment From: hantsy

@wilkinsona Updated the original post.

Comment From: wilkinsona

Thanks but I'm afraid that I still don't see the benefit. Both ApplicationRunner and CommandLineRunner are synchronous contracts that are run in the order defined by their @Order annotation or Ordered implementation. You're proposing a new CoroutingApplicationRunner API. How would this work with other ApplicationRunner implementations ordered to run before or after the runner that's using a coroutine?

Comment From: hantsy

In the past months, I worked on a Spring WebFlux/R2dbc/Kotlin Coroutines project, but found a lot of Spring features still lack Coroutines support which are provided in the legacy project https://github.com/konrad-kaminski/spring-kotlin-coroutine, such as ApplicationEvent/EventListener, cache, schedule etc.

Spring 6.0.5 has just added a Coroutine variant CoWebFilter as alternative of WebFilter.

I just want my codes look more consistent when using Kotlin Coroutines. Maybe it is easier if Spring add Coroutines to Configurations/beans declarations.

Comment From: wilkinsona

Coroutines are a good fit for WebFilter which is already part of the Reactor-based WebFlux stack. As I explained above, I don't think they're a good fit for the ApplicationRunner and CommandLineRunner contract so I'm going to close this issue accordingly. Thanks anyway for the suggestion.