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

Limit ref() and provider() visibility in Kotlin DSL [SPR-17648] #22177

Closed
spring-issuemaster opened this issue Jan 8, 2019 · 0 comments
Assignees
Milestone

Comments

@spring-issuemaster
Copy link
Collaborator

@spring-issuemaster spring-issuemaster commented Jan 8, 2019

Sébastien Deleuze opened SPR-17648 and commented

Current Kotlin bean DSL makes it possible to shoot yourself in the foot easily by allowing to write:

beans {
	val bar = ref<Bar>()
	bean<Foo> { Foo(bar)}
}

Which will trigger java.lang.IllegalStateException: org.springframework.context.support.GenericApplicationContext@2c532cd8 has not been refreshed yet.

We should only expose ref() and provider() in the supplier responsible for providing the bean, since it will be call at the right time of the lifecycle.

We could maybe introduce some kind of BeanDefinitionContext:

open class BeanDefinitionContext(@PublishedApi internal val context: GenericApplicationContext) {

    inline fun <reified T : Any> ref(name: String? = null): T = when (name) {
        null -> context.getBean(T::class.java)
        else -> context.getBean(name, T::class.java)
    }

    inline fun <reified T : Any> provider() : ObjectProvider<T> = context.getBeanProvider()
}

And change crossinline function: () -> T to crossinline function: BeanDefinitionContext.() -> T in the bean supplier variant.

Additional notes:

  • We should maybe use this opportunity to provide direct access to ObjectProvider methods, expose Sequence instead of Stream, etc.
  • We should maybe think about providing a router() variant that leverages this BeanDefinitionContext since this is a common use case.

No further details from SPR-17648

@spring-issuemaster spring-issuemaster added this to the 5.2 RC1 milestone Jan 11, 2019
sdeleuze added a commit to sdeleuze/spring-framework that referenced this issue Feb 5, 2019
This commit makes ref() and provider() only available from
inside the bean lambda and not from the root level of the beans DSL.

Closes spring-projectsgh-22177
sdeleuze added a commit to sdeleuze/spring-framework that referenced this issue Feb 5, 2019
This commit makes ref() and provider() only available from
inside the bean lambda and not from the root level of the beans DSL.

Closes spring-projectsgh-22177
@sdeleuze sdeleuze closed this in 0ad9349 Feb 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.