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

Bean registration with Kotlin DSL using callable reference with autowired parameters

    XMLWordPrintable

    Details

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

      Description

      I had this feature in mind for a long time, but this seems that autowiring of Kotlin top level functions is straightforward and require no reflection, but just leveraging Kotlin type system.

      inline fun <reified T : Any> BeanDefinitionDsl.bean(crossinline f: Function0<T>): Any = context.registerBean { f.invoke() }
      
      inline fun <reified A : Any, reified T : Any> BeanDefinitionDsl.bean(crossinline f: Function1<A, T>): Any = context.registerBean { f.invoke(context.getBean()) }
      
      inline fun <reified A : Any, reified B : Any, reified T : Any> BeanDefinitionDsl.bean(crossinline f: Function2<A, B, T>): Any = context.registerBean { f.invoke(context.getBean(), context.getBean()) }
      
      // To be defined the number of parameter we want to support
      

      A typical use case is autowiring of router parameters, for example:

      fun routes(userHandler: UserHandler) = router {
      	GET("/", userHandler::listView)
      	GET("/api/user", userHandler::listApi)
      	GET("/conf", userHandler::conf)
      }
      
      beans {
      	bean<UserRepository>()
      	bean<UserHandler>()
      	bean(::routes)
      }
      

      That would be a very nice Spring Framework 5.2 feature IMO

        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