-
Notifications
You must be signed in to change notification settings - Fork 38.6k
Description
Sébastien Deleuze opened SPR-15755 and commented
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:
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:
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 #18353 issue).