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

Introduce Kotlin Functional bean registration DSL

    Details

    • Type: New Feature
    • Status: Closed
    • Priority: Minor
    • Resolution: Complete
    • Affects Version/s: None
    • Fix Version/s: 5.0 RC3
    • Component/s: None
    • Labels:
      None
    • Last commented by a User:
      true

      Description

      As a follow-up of Spring Framework 5.0 bean registration Kotlin extensions, close to what we did for the Kotlin functional WebFlux DSL and similar to Groovy bean configuration DSL (but very different in term of implementation since here no internal XML-based application context is involved), this issue is about introducing a very lightweight Kotlin DSL for function bean registration.

      Current Kotlin extensions in Spring Framework 5 RC2 allow to write this Java version

      GenericApplicationContext context = new GenericApplicationContext();
      context.registerBean(Foo.class);
      context.registerBean(Bar.class, () -> new 
      	Bar(context.getBean(Foo.class))
      );
      

      Like following in Kotlin:

      Unable to find source-code formatter for language: kotlin. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      GenericApplicationContext {
          registerBean<Foo>()
          registerBean { Bar(it.getBean<Foo>()) }
      }
      

      While shorter, this syntax is not declarative (verbs like register are used), require to choose and expose a specific ApplicationContext implementation which is not very common in Spring Boot applications for example and could be more easy to write and read.

      My proposal is to provide a lightweight Kotlin DSL to allow an even more idiomatic way to register bean in Kotlin:

      Unable to find source-code formatter for language: kotlin. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      beans {
          bean<Foo>()
          bean { Bar(it.ref<Foo>()) }
      }
      

      This Kotlin functional bean registration DSL would return a Consumer<GenericApplicationContext> allowing to register beans on an existing application context like the one created by Boot (see also the related SPR-13779 issue).

        Attachments

          Activity

            People

            • Assignee:
              sdeleuze Sébastien Deleuze
              Reporter:
              sdeleuze Sébastien Deleuze
              Last updater:
              Spring Issuemaster
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Days since last comment:
                31 weeks, 3 days ago