Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-17648

Limit ref() and provider() visibility in Kotlin DSL

    XMLWordPrintable

    Details

    • Type: Improvement
    • Status: Open
    • Priority: Major
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: 5.2 RC1
    • Component/s: Core
    • Labels:
    • Last commented by a User:
      true

      Description

      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: [email protected]532cd8 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.

        Attachments

          Activity

            People

            Assignee:
            sdeleuze S├ębastien Deleuze
            Reporter:
            sdeleuze S├ębastien Deleuze
            Last updater:
            Spring Issues Spring Issues
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Dates

              Created:
              Updated:
              Days since last comment:
              3 years, 24 weeks, 1 day ago