Skip to content
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

Bean registration with Kotlin DSL using callable reference with autowired parameters [SPR-17312] #21845

Closed
spring-projects-issues opened this issue Sep 29, 2018 · 0 comments
Assignees
Labels
type: enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

@spring-projects-issues spring-projects-issues commented Sep 29, 2018

Sébastien Deleuze opened SPR-17312 and commented

I had this feature in mind for a long time, but this seems that autowiring of Kotlin top level functions is straightforward and require no reflection, but just leveraging Kotlin type system.

inline fun <reified T : Any> BeanDefinitionDsl.bean(crossinline f: Function0<T>): Any = context.registerBean { f.invoke() }

inline fun <reified A : Any, reified T : Any> BeanDefinitionDsl.bean(crossinline f: Function1<A, T>): Any = context.registerBean { f.invoke(context.getBean()) }

inline fun <reified A : Any, reified B : Any, reified T : Any> BeanDefinitionDsl.bean(crossinline f: Function2<A, B, T>): Any = context.registerBean { f.invoke(context.getBean(), context.getBean()) }

// To be defined the number of parameter we want to support

A typical use case is autowiring of router parameters, for example:

fun routes(userHandler: UserHandler) = router {
	GET("/", userHandler::listView)
	GET("/api/user", userHandler::listApi)
	GET("/conf", userHandler::conf)
}

beans {
	bean<UserRepository>()
	bean<UserHandler>()
	bean(::routes)
}

That would be a very nice Spring Framework 5.2 feature IMO ;-)


No further details from SPR-17312

@spring-projects-issues spring-projects-issues added the type: enhancement label Jan 11, 2019
@spring-projects-issues spring-projects-issues added this to the 5.2 RC1 milestone Jan 11, 2019
sdeleuze added a commit to sdeleuze/spring-framework that referenced this issue Mar 27, 2019
This commit updates Kotlin beans DSL in order to support
creating beans using callable references with autowired
parameters. Type resolution is implemented using Kotlin
reified type parameters without requiring reflection.

Closes spring-projectsgh-21845
sdeleuze added a commit to sdeleuze/spring-framework that referenced this issue Mar 27, 2019
This commit updates Kotlin beans DSL in order to support
creating beans using callable references with autowired
parameters. Type resolution is implemented using Kotlin
reified type parameters without requiring reflection.

Closes spring-projectsgh-21845
sdeleuze added a commit to sdeleuze/spring-framework that referenced this issue Mar 27, 2019
This commit updates Kotlin beans DSL in order to support
creating beans using callable references with autowired
parameters. Type resolution is implemented using Kotlin
reified type parameters without requiring reflection.

Closes spring-projectsgh-21845
sdeleuze added a commit to spring-projects-experimental/spring-fu that referenced this issue Mar 28, 2019
This is now available in Spring Framework, see
spring-projects/spring-framework#21845.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement
Projects
None yet
Development

No branches or pull requests

2 participants