Affects: Any


Spring introduced first-class support to kotlin and coroutines and while almost everything works flawlessly I really miss the support for coroutines in the @EventListener.

I used in the past spring-kotlin-coroutine however this project is not updated for 3 years so I wonder if such support could be added to core Spring.

Comment From: martinformi

It would be really helpful, do you have any working implementation that could replace it for now?

Comment From: hantsy

The original Spring Kotlin Corutines has great Kotlin Coroutines support, unfortunately it is discontinued. A lot of features are not support in current Spring framework although Spring has supported Kotlin for years.

I used Kotlin Coroutines/Spring Boot in my project, and I found there are still some problems when using Coroutines in events, cache, schedule/async and Spring Security DSL config. I had to wrap the corourtines codes with a runBlocking to use it in the existing facilities of Spring.

Comment From: sdeleuze

Spring supports Coroutines where it support reactive types, and since #21831 supports that use case, we should indeed support this use case.

Comment From: hantsy

@sdeleuze I would like get the following cases working in a Spring/Kotlin Coroutines project.

  1. Allow a suspend main fun for the application entry.
  2. Coroutines version of ApplicationEventListener, @EventListener on a suspend fun or a fun returns a Kotlin Flow.
  3. Make cache annotations work on any suspend fun or Flow return type.
  4. Make it work in Spring Security Kotlin DSL http{}(similar to route, there is coRoute for coroutines)
  5. provides a coroutines versions of CommandLineRunner, ApplicationRunner, ReactiveAuditorAware, ReactiveEntityCallback, etc.

Comment From: sdeleuze

From my point of view, Spring should not support suspend everywhere, but I fully understand it is not always easy to draw the line between what should be supported and what should not. Notice that this question is not specific to Kotlin Coroutines, but is also true for our Reactive support.

Spring should provides Coroutines support where Reactive types are supported. For example, we recently added support for Reactive/Coroutines to @Scheduled (see #29924).

For event listeners, we indeed support Reactive types on event listeners so it would make sense to support Coroutines as well (see https://github.com/spring-projects/spring-framework/issues/27515 related issue even if more focused on transactional support).

So let's try to make that happen early in 6.1.x release cycle (tentatively planned for RC2 but that may be postponed depending on my bandwidth).

Comment From: hantsy

Great to see schedule, cache, event listener to get Reactive/Coroutines support in Spring 6.1.

Comment From: rotilho

@sdeleuze awesome work! Super thanks!