As a follow-up of the ApplicationContext Kotlin extensions, close to the Kotlin functional WebFlux DSL and similar to the Groovy bean configuration DSL (but very different in term of implementation since no internal XML-based application context is involved), this commit is about introducing a lightweight Kotlin DSL for functional bean registration.
It allows declaring beans as following:
beans {
bean<Foo>()
profile("bar") {
bean<Bar>("bar")
}
environment({it.activeProfiles.contains("baz")}) {
bean { Baz(it.ref()) }
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 a fewer
entries in the auto-complete
- Declarative syntax instead of functions with verbs like registerBeans
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>
This DSL effectively replaces ApplicationContext
Kotlin extensions as
the recommended way to register beans in a functional way with Kotlin.
Issue: SPR-15755
Comment From: Tradunsky
:+1:
Comment From: sdeleuze
Merged.