sdeleuze
07/11/2017, 12:43 PMApplicationContext
extensions (see https://spring.io/blog/2017/01/04/introducing-kotlin-support-in-spring-framework-5-0#functional-bean-registration-with-kotlin). While the syntax is much shorter than in Java, I was still puzzled by the fact this way of declaring beans was not declarative, and would maybe be worth introducing a lightweight Kotlin DSL to get a 100% idomatic Kotlin syntax. I plan to introduce it as part of that pull request (https://github.com/spring-projects/spring-framework/pull/1473) that allows to declare your beans as following:
beans {
bean<Foo>()
bean<Bar>("bar")
if (env.activeProfiles.contains("baz")) {
bean { Baz(it.ref<Bar>()) }
bean { Baz(it.ref("bar")) }
}
}
Advantages compared to Regular ApplicationContext
API are:
- No exposure of low-level ApplicationContext
API Focused DSL easier to read, but also easier to write with fewer entries in the auto-complete
- Declarative syntax instead of functions with verbs like registerBean
while still allowing programmatic registration of beans with if expressions or for loops
- Such DSL is idiomatic in Kotlin
- No need to have an ApplicationContext
instance to write how you register your beans since beans { }
DSL is conceptually a Consumer<ApplicationContext>
Any feedbacks is welcome. Do you like such DSL? Are you ok with the syntax? Any issue to raise?