Skip to content

Introduce Kotlin Functional bean registration DSL [SPR-15755] #20310

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
spring-projects-issues opened this issue Jul 10, 2017 · 0 comments
Closed
Assignees
Labels
type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Jul 10, 2017

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).


Referenced from: commits 1f01146, f72e0da

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants