Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initialize beans with coroutine context #26553

Closed
tomfi opened this issue Feb 16, 2021 · 4 comments
Closed

Initialize beans with coroutine context #26553

tomfi opened this issue Feb 16, 2021 · 4 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: declined A suggestion or change that we don't feel we should currently apply theme: kotlin An issue related to Kotlin support

Comments

@tomfi
Copy link

tomfi commented Feb 16, 2021

Currently there are a few ways to initialize beans, but i did not find a way to initialize a class that require a coroutine context.

For example we can take the following class:

class FooService private constructor(...) {
  companion object {
    suspend fun create(...): FooService {
      return FooService(...)
    }
  }
}

To initialize this service as a bean, it can not be marked as a @Component and should be registered as a @Bean. It would be great if the following was possible:

Register @Bean with a suspend function. e.g.

@Configuration
class FooConfiguration {
  @Bean
  suspend fun fooService(...) {
    return FooService.create(...)
  }
}

Add bean using BeanDefinitionDsl. e.g.

coBeans {
  bean {
    FooService.create(...)
  }
}.initialize(applicationContext)

This also leads to other places where a coroutine context would be helpful, or if the infrastructure would be based on callbacks / futures. For example in my project we have a use-case where we use the beans dsl to register beans triggered from a ApplicationListener<ContextRefreshedEvent>. It would be great if onApplicationEvent could also have a suspend version so the coBeans dsl could be used from there as well.

Currently the only way i could find to overcome this is using runBlocking in those places, which is not recommended and can cause problems.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Feb 16, 2021
@sdeleuze
Copy link
Contributor

Related to #19487 and #21025.

@rstoyanchev rstoyanchev added the in: core Issues in core modules (aop, beans, core, context, expression) label Nov 11, 2021
@snicoll snicoll added the theme: kotlin An issue related to Kotlin support label Sep 28, 2023
@sdeleuze sdeleuze self-assigned this Oct 3, 2023
@sdeleuze
Copy link
Contributor

sdeleuze commented Oct 3, 2023

@tomfi Sorry for the delay, what concrete benefits would you expect compared to using runBlocking?

@sdeleuze sdeleuze added the status: waiting-for-feedback We need additional information before we can continue label Oct 3, 2023
@tomfi
Copy link
Author

tomfi commented Oct 3, 2023

@sdeleuze the main benefit is to avoid using a rather non-safe kotlin coroutine bridge, especially in a non-blocking world where you want to avoid blocking as much as possible in any phase of your code/application.

From the official docs of runBlocking:

It is designed to bridge regular blocking code to libraries that are written in suspending style, to be used in main functions and in tests.

If it can be avoided, it will be more aligned with non-blocking code and kotlin coroutines as a whole.

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Oct 3, 2023
@sdeleuze
Copy link
Contributor

I am not sure we should apply suspending/async semantics everywhere. When processing requests, it makes sense. For creating beans, I don't think it makes sense to provide special support for that. Another line of argument is that we don't provide special support for Mono beans, and Reactive and Coroutines support are almost everywhere in sync. As a consequence, I decline this proposal and advise to use runBlocking for this use case where we don't provide Reactive/Coroutines support.

@sdeleuze sdeleuze closed this as not planned Won't fix, can't repro, duplicate, stale Oct 10, 2023
@sdeleuze sdeleuze added status: declined A suggestion or change that we don't feel we should currently apply and removed status: waiting-for-triage An issue we've not yet triaged or decided on status: feedback-provided Feedback has been provided labels Oct 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: declined A suggestion or change that we don't feel we should currently apply theme: kotlin An issue related to Kotlin support
Projects
None yet
Development

No branches or pull requests

5 participants