Support creating beans from Kotlin callable references

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 gh-21845
This commit is contained in:
Sebastien Deleuze
2019-03-27 14:30:39 +01:00
parent c9857702e4
commit fb1da01410
3 changed files with 867 additions and 5 deletions

View File

@@ -235,6 +235,825 @@ open class BeanDefinitionDsl(private val init: BeanDefinitionDsl.() -> Unit,
context.registerBean(beanName, T::class.java, Supplier { function.invoke(BeanSupplierContext(context)) }, customizer)
}
/**
* Declare a bean definition using the given callable reference with 1 parameter
* autowired by type for obtaining a new instance.
*
* @param f the callable reference
* @param name the name of the bean
* @param scope Override the target scope of this bean, specifying a new scope name.
* @param isLazyInit Set whether this bean should be lazily initialized.
* @param isPrimary Set whether this bean is a primary autowire candidate.
* @param isAutowireCandidate Set whether this bean is a candidate for getting
* autowired into some other bean.
* @param initMethodName Set the name of the initializer method
* @param destroyMethodName Set the name of the destroy method
* @param description Set a human-readable description of this bean definition
* @param role Set the role hint for this bean definition
* @see GenericApplicationContext.registerBean
* @see org.springframework.beans.factory.config.BeanDefinition
* @since 5.2
*/
inline fun <reified T: Any, reified A: Any>
bean(crossinline f: (A) -> T,
name: String? = null,
scope: BeanDefinitionDsl.Scope? = null,
isLazyInit: Boolean? = null,
isPrimary: Boolean? = null,
isAutowireCandidate: Boolean? = null,
initMethodName: String? = null,
destroyMethodName: String? = null,
description: String? = null,
role: BeanDefinitionDsl.Role? = null) {
bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) {
f.invoke(ref())
}
}
/**
* Declare a bean definition using the given callable reference with 2 parameters
* autowired by type for obtaining a new instance.
*
* @param f the callable reference
* @param name the name of the bean
* @param scope Override the target scope of this bean, specifying a new scope name.
* @param isLazyInit Set whether this bean should be lazily initialized.
* @param isPrimary Set whether this bean is a primary autowire candidate.
* @param isAutowireCandidate Set whether this bean is a candidate for getting
* autowired into some other bean.
* @param initMethodName Set the name of the initializer method
* @param destroyMethodName Set the name of the destroy method
* @param description Set a human-readable description of this bean definition
* @param role Set the role hint for this bean definition
* @see GenericApplicationContext.registerBean
* @see org.springframework.beans.factory.config.BeanDefinition
* @since 5.2
*/
inline fun <reified T: Any, reified A: Any, reified B: Any>
bean(crossinline f: (A, B) -> T,
name: String? = null,
scope: BeanDefinitionDsl.Scope? = null,
isLazyInit: Boolean? = null,
isPrimary: Boolean? = null,
isAutowireCandidate: Boolean? = null,
initMethodName: String? = null,
destroyMethodName: String? = null,
description: String? = null,
role: BeanDefinitionDsl.Role? = null) {
bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) {
f.invoke(ref(), ref())
}
}
/**
* Declare a bean definition using the given callable reference with 3 parameters
* autowired by type for obtaining a new instance.
*
* @param f the callable reference
* @param name the name of the bean
* @param scope Override the target scope of this bean, specifying a new scope name.
* @param isLazyInit Set whether this bean should be lazily initialized.
* @param isPrimary Set whether this bean is a primary autowire candidate.
* @param isAutowireCandidate Set whether this bean is a candidate for getting
* autowired into some other bean.
* @param initMethodName Set the name of the initializer method
* @param destroyMethodName Set the name of the destroy method
* @param description Set a human-readable description of this bean definition
* @param role Set the role hint for this bean definition
* @see GenericApplicationContext.registerBean
* @see org.springframework.beans.factory.config.BeanDefinition
* @since 5.2
*/
inline fun <reified T: Any, reified A: Any, reified B: Any, reified C: Any>
bean(crossinline f: (A, B, C) -> T,
name: String? = null,
scope: BeanDefinitionDsl.Scope? = null,
isLazyInit: Boolean? = null,
isPrimary: Boolean? = null,
isAutowireCandidate: Boolean? = null,
initMethodName: String? = null,
destroyMethodName: String? = null,
description: String? = null,
role: BeanDefinitionDsl.Role? = null) {
bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) {
f.invoke(ref(), ref(), ref())
}
}
/**
* Declare a bean definition using the given callable reference with 4 parameters
* autowired by type for obtaining a new instance.
*
* @param f the callable reference
* @param name the name of the bean
* @param scope Override the target scope of this bean, specifying a new scope name.
* @param isLazyInit Set whether this bean should be lazily initialized.
* @param isPrimary Set whether this bean is a primary autowire candidate.
* @param isAutowireCandidate Set whether this bean is a candidate for getting
* autowired into some other bean.
* @param initMethodName Set the name of the initializer method
* @param destroyMethodName Set the name of the destroy method
* @param description Set a human-readable description of this bean definition
* @param role Set the role hint for this bean definition
* @see GenericApplicationContext.registerBean
* @see org.springframework.beans.factory.config.BeanDefinition
* @since 5.2
*/
inline fun <reified T: Any, reified A: Any, reified B: Any, reified C: Any, reified D: Any>
bean(crossinline f: (A, B, C, D) -> T,
name: String? = null,
scope: BeanDefinitionDsl.Scope? = null,
isLazyInit: Boolean? = null,
isPrimary: Boolean? = null,
isAutowireCandidate: Boolean? = null,
initMethodName: String? = null,
destroyMethodName: String? = null,
description: String? = null,
role: BeanDefinitionDsl.Role? = null) {
bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) {
f.invoke(ref(), ref(), ref(), ref())
}
}
/**
* Declare a bean definition using the given callable reference with 5 parameters
* autowired by type for obtaining a new instance.
*
* @param f the callable reference
* @param name the name of the bean
* @param scope Override the target scope of this bean, specifying a new scope name.
* @param isLazyInit Set whether this bean should be lazily initialized.
* @param isPrimary Set whether this bean is a primary autowire candidate.
* @param isAutowireCandidate Set whether this bean is a candidate for getting
* autowired into some other bean.
* @param initMethodName Set the name of the initializer method
* @param destroyMethodName Set the name of the destroy method
* @param description Set a human-readable description of this bean definition
* @param role Set the role hint for this bean definition
* @see GenericApplicationContext.registerBean
* @see org.springframework.beans.factory.config.BeanDefinition
* @since 5.2
*/
inline fun <reified T: Any, reified A: Any, reified B: Any, reified C: Any, reified D: Any, reified E: Any>
bean(crossinline f: (A, B, C, D, E) -> T,
name: String? = null,
scope: BeanDefinitionDsl.Scope? = null,
isLazyInit: Boolean? = null,
isPrimary: Boolean? = null,
isAutowireCandidate: Boolean? = null,
initMethodName: String? = null,
destroyMethodName: String? = null,
description: String? = null,
role: BeanDefinitionDsl.Role? = null) {
bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) {
f.invoke(ref(), ref(), ref(), ref(), ref())
}
}
/**
* Declare a bean definition using the given callable reference with 6 parameters
* autowired by type for obtaining a new instance.
*
* @param f the callable reference
* @param name the name of the bean
* @param scope Override the target scope of this bean, specifying a new scope name.
* @param isLazyInit Set whether this bean should be lazily initialized.
* @param isPrimary Set whether this bean is a primary autowire candidate.
* @param isAutowireCandidate Set whether this bean is a candidate for getting
* autowired into some other bean.
* @param initMethodName Set the name of the initializer method
* @param destroyMethodName Set the name of the destroy method
* @param description Set a human-readable description of this bean definition
* @param role Set the role hint for this bean definition
* @see GenericApplicationContext.registerBean
* @see org.springframework.beans.factory.config.BeanDefinition
* @since 5.2
*/
inline fun <reified T: Any, reified A: Any, reified B: Any, reified C: Any, reified D: Any, reified E: Any, reified F: Any>
bean(crossinline f: (A, B, C, D, E, F) -> T,
name: String? = null,
scope: BeanDefinitionDsl.Scope? = null,
isLazyInit: Boolean? = null,
isPrimary: Boolean? = null,
isAutowireCandidate: Boolean? = null,
initMethodName: String? = null,
destroyMethodName: String? = null,
description: String? = null,
role: BeanDefinitionDsl.Role? = null) {
bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) {
f.invoke(ref(), ref(), ref(), ref(), ref(), ref())
}
}
/**
* Declare a bean definition using the given callable reference with 7 parameters
* autowired by type for obtaining a new instance.
*
* @param f the callable reference
* @param name the name of the bean
* @param scope Override the target scope of this bean, specifying a new scope name.
* @param isLazyInit Set whether this bean should be lazily initialized.
* @param isPrimary Set whether this bean is a primary autowire candidate.
* @param isAutowireCandidate Set whether this bean is a candidate for getting
* autowired into some other bean.
* @param initMethodName Set the name of the initializer method
* @param destroyMethodName Set the name of the destroy method
* @param description Set a human-readable description of this bean definition
* @param role Set the role hint for this bean definition
* @see GenericApplicationContext.registerBean
* @see org.springframework.beans.factory.config.BeanDefinition
* @since 5.2
*/
inline fun <reified T: Any, reified A: Any, reified B: Any, reified C: Any, reified D: Any, reified E: Any, reified F: Any,
reified G: Any>
bean(crossinline f: (A, B, C, D, E, F, G) -> T,
name: String? = null,
scope: BeanDefinitionDsl.Scope? = null,
isLazyInit: Boolean? = null,
isPrimary: Boolean? = null,
isAutowireCandidate: Boolean? = null,
initMethodName: String? = null,
destroyMethodName: String? = null,
description: String? = null,
role: BeanDefinitionDsl.Role? = null) {
bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) {
f.invoke(ref(), ref(), ref(), ref(), ref(), ref(), ref())
}
}
/**
* Declare a bean definition using the given callable reference with 8 parameters
* autowired by type for obtaining a new instance.
*
* @param f the callable reference
* @param name the name of the bean
* @param scope Override the target scope of this bean, specifying a new scope name.
* @param isLazyInit Set whether this bean should be lazily initialized.
* @param isPrimary Set whether this bean is a primary autowire candidate.
* @param isAutowireCandidate Set whether this bean is a candidate for getting
* autowired into some other bean.
* @param initMethodName Set the name of the initializer method
* @param destroyMethodName Set the name of the destroy method
* @param description Set a human-readable description of this bean definition
* @param role Set the role hint for this bean definition
* @see GenericApplicationContext.registerBean
* @see org.springframework.beans.factory.config.BeanDefinition
* @since 5.2
*/
inline fun <reified T: Any, reified A: Any, reified B: Any, reified C: Any, reified D: Any, reified E: Any, reified F: Any,
reified G: Any, reified H: Any>
bean(crossinline f: (A, B, C, D, E, F, G, H) -> T,
name: String? = null,
scope: BeanDefinitionDsl.Scope? = null,
isLazyInit: Boolean? = null,
isPrimary: Boolean? = null,
isAutowireCandidate: Boolean? = null,
initMethodName: String? = null,
destroyMethodName: String? = null,
description: String? = null,
role: BeanDefinitionDsl.Role? = null) {
bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) {
f.invoke(ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref())
}
}
/**
* Declare a bean definition using the given callable reference with 9 parameters
* autowired by type for obtaining a new instance.
*
* @param f the callable reference
* @param name the name of the bean
* @param scope Override the target scope of this bean, specifying a new scope name.
* @param isLazyInit Set whether this bean should be lazily initialized.
* @param isPrimary Set whether this bean is a primary autowire candidate.
* @param isAutowireCandidate Set whether this bean is a candidate for getting
* autowired into some other bean.
* @param initMethodName Set the name of the initializer method
* @param destroyMethodName Set the name of the destroy method
* @param description Set a human-readable description of this bean definition
* @param role Set the role hint for this bean definition
* @see GenericApplicationContext.registerBean
* @see org.springframework.beans.factory.config.BeanDefinition
* @since 5.2
*/
inline fun <reified T: Any, reified A: Any, reified B: Any, reified C: Any, reified D: Any, reified E: Any, reified F: Any,
reified G: Any, reified H: Any, reified I: Any>
bean(crossinline f: (A, B, C, D, E, F, G, H, I) -> T,
name: String? = null,
scope: BeanDefinitionDsl.Scope? = null,
isLazyInit: Boolean? = null,
isPrimary: Boolean? = null,
isAutowireCandidate: Boolean? = null,
initMethodName: String? = null,
destroyMethodName: String? = null,
description: String? = null,
role: BeanDefinitionDsl.Role? = null) {
bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) {
f.invoke(ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref())
}
}
/**
* Declare a bean definition using the given callable reference with 10 parameters
* autowired by type for obtaining a new instance.
*
* @param f the callable reference
* @param name the name of the bean
* @param scope Override the target scope of this bean, specifying a new scope name.
* @param isLazyInit Set whether this bean should be lazily initialized.
* @param isPrimary Set whether this bean is a primary autowire candidate.
* @param isAutowireCandidate Set whether this bean is a candidate for getting
* autowired into some other bean.
* @param initMethodName Set the name of the initializer method
* @param destroyMethodName Set the name of the destroy method
* @param description Set a human-readable description of this bean definition
* @param role Set the role hint for this bean definition
* @see GenericApplicationContext.registerBean
* @see org.springframework.beans.factory.config.BeanDefinition
* @since 5.2
*/
inline fun <reified T: Any, reified A: Any, reified B: Any, reified C: Any, reified D: Any, reified E: Any, reified F: Any,
reified G: Any, reified H: Any, reified I: Any, reified J: Any>
bean(crossinline f: (A, B, C, D, E, F, G, H, I, J) -> T,
name: String? = null,
scope: BeanDefinitionDsl.Scope? = null,
isLazyInit: Boolean? = null,
isPrimary: Boolean? = null,
isAutowireCandidate: Boolean? = null,
initMethodName: String? = null,
destroyMethodName: String? = null,
description: String? = null,
role: BeanDefinitionDsl.Role? = null) {
bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) {
f.invoke(ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref())
}
}
/**
* Declare a bean definition using the given callable reference with 11 parameters
* autowired by type for obtaining a new instance.
*
* @param f the callable reference
* @param name the name of the bean
* @param scope Override the target scope of this bean, specifying a new scope name.
* @param isLazyInit Set whether this bean should be lazily initialized.
* @param isPrimary Set whether this bean is a primary autowire candidate.
* @param isAutowireCandidate Set whether this bean is a candidate for getting
* autowired into some other bean.
* @param initMethodName Set the name of the initializer method
* @param destroyMethodName Set the name of the destroy method
* @param description Set a human-readable description of this bean definition
* @param role Set the role hint for this bean definition
* @see GenericApplicationContext.registerBean
* @see org.springframework.beans.factory.config.BeanDefinition
* @since 5.2
*/
inline fun <reified T: Any, reified A: Any, reified B: Any, reified C: Any, reified D: Any, reified E: Any, reified F: Any,
reified G: Any, reified H: Any, reified I: Any, reified J: Any, reified K: Any>
bean(crossinline f: (A, B, C, D, E, F, G, H, I, J, K) -> T,
name: String? = null,
scope: BeanDefinitionDsl.Scope? = null,
isLazyInit: Boolean? = null,
isPrimary: Boolean? = null,
isAutowireCandidate: Boolean? = null,
initMethodName: String? = null,
destroyMethodName: String? = null,
description: String? = null,
role: BeanDefinitionDsl.Role? = null) {
bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) {
f.invoke(ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref())
}
}
/**
* Declare a bean definition using the given callable reference with 12 parameters
* autowired by type for obtaining a new instance.
*
* @param f the callable reference
* @param name the name of the bean
* @param scope Override the target scope of this bean, specifying a new scope name.
* @param isLazyInit Set whether this bean should be lazily initialized.
* @param isPrimary Set whether this bean is a primary autowire candidate.
* @param isAutowireCandidate Set whether this bean is a candidate for getting
* autowired into some other bean.
* @param initMethodName Set the name of the initializer method
* @param destroyMethodName Set the name of the destroy method
* @param description Set a human-readable description of this bean definition
* @param role Set the role hint for this bean definition
* @see GenericApplicationContext.registerBean
* @see org.springframework.beans.factory.config.BeanDefinition
* @since 5.2
*/
inline fun <reified T: Any, reified A: Any, reified B: Any, reified C: Any, reified D: Any, reified E: Any, reified F: Any,
reified G: Any, reified H: Any, reified I: Any, reified J: Any, reified K: Any, reified L: Any>
bean(crossinline f: (A, B, C, D, E, F, G, H, I, J, K, L) -> T,
name: String? = null,
scope: BeanDefinitionDsl.Scope? = null,
isLazyInit: Boolean? = null,
isPrimary: Boolean? = null,
isAutowireCandidate: Boolean? = null,
initMethodName: String? = null,
destroyMethodName: String? = null,
description: String? = null,
role: BeanDefinitionDsl.Role? = null) {
bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) {
f.invoke(ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref())
}
}
/**
* Declare a bean definition using the given callable reference with 13 parameters
* autowired by type for obtaining a new instance.
*
* @param f the callable reference
* @param name the name of the bean
* @param scope Override the target scope of this bean, specifying a new scope name.
* @param isLazyInit Set whether this bean should be lazily initialized.
* @param isPrimary Set whether this bean is a primary autowire candidate.
* @param isAutowireCandidate Set whether this bean is a candidate for getting
* autowired into some other bean.
* @param initMethodName Set the name of the initializer method
* @param destroyMethodName Set the name of the destroy method
* @param description Set a human-readable description of this bean definition
* @param role Set the role hint for this bean definition
* @see GenericApplicationContext.registerBean
* @see org.springframework.beans.factory.config.BeanDefinition
* @since 5.2
*/
inline fun <reified T: Any, reified A: Any, reified B: Any, reified C: Any, reified D: Any, reified E: Any, reified F: Any,
reified G: Any, reified H: Any, reified I: Any, reified J: Any, reified K: Any, reified L: Any, reified M: Any>
bean(crossinline f: (A, B, C, D, E, F, G, H, I, J, K, L, M) -> T,
name: String? = null,
scope: BeanDefinitionDsl.Scope? = null,
isLazyInit: Boolean? = null,
isPrimary: Boolean? = null,
isAutowireCandidate: Boolean? = null,
initMethodName: String? = null,
destroyMethodName: String? = null,
description: String? = null,
role: BeanDefinitionDsl.Role? = null) {
bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) {
f.invoke(ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref())
}
}
/**
* Declare a bean definition using the given callable reference with 14 parameters
* autowired by type for obtaining a new instance.
*
* @param f the callable reference
* @param name the name of the bean
* @param scope Override the target scope of this bean, specifying a new scope name.
* @param isLazyInit Set whether this bean should be lazily initialized.
* @param isPrimary Set whether this bean is a primary autowire candidate.
* @param isAutowireCandidate Set whether this bean is a candidate for getting
* autowired into some other bean.
* @param initMethodName Set the name of the initializer method
* @param destroyMethodName Set the name of the destroy method
* @param description Set a human-readable description of this bean definition
* @param role Set the role hint for this bean definition
* @see GenericApplicationContext.registerBean
* @see org.springframework.beans.factory.config.BeanDefinition
* @since 5.2
*/
inline fun <reified T: Any, reified A: Any, reified B: Any, reified C: Any, reified D: Any, reified E: Any, reified F: Any,
reified G: Any, reified H: Any, reified I: Any, reified J: Any, reified K: Any, reified L: Any, reified M: Any,
reified N: Any>
bean(crossinline f: (A, B, C, D, E, F, G, H, I, J, K, L, M, N) -> T,
name: String? = null,
scope: BeanDefinitionDsl.Scope? = null,
isLazyInit: Boolean? = null,
isPrimary: Boolean? = null,
isAutowireCandidate: Boolean? = null,
initMethodName: String? = null,
destroyMethodName: String? = null,
description: String? = null,
role: BeanDefinitionDsl.Role? = null) {
bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) {
f.invoke(ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref())
}
}
/**
* Declare a bean definition using the given callable reference with 15 parameters
* autowired by type for obtaining a new instance.
*
* @param f the callable reference
* @param name the name of the bean
* @param scope Override the target scope of this bean, specifying a new scope name.
* @param isLazyInit Set whether this bean should be lazily initialized.
* @param isPrimary Set whether this bean is a primary autowire candidate.
* @param isAutowireCandidate Set whether this bean is a candidate for getting
* autowired into some other bean.
* @param initMethodName Set the name of the initializer method
* @param destroyMethodName Set the name of the destroy method
* @param description Set a human-readable description of this bean definition
* @param role Set the role hint for this bean definition
* @see GenericApplicationContext.registerBean
* @see org.springframework.beans.factory.config.BeanDefinition
* @since 5.2
*/
inline fun <reified T: Any, reified A: Any, reified B: Any, reified C: Any, reified D: Any, reified E: Any, reified F: Any,
reified G: Any, reified H: Any, reified I: Any, reified J: Any, reified K: Any, reified L: Any, reified M: Any,
reified N: Any, reified O: Any>
bean(crossinline f: (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O) -> T,
name: String? = null,
scope: BeanDefinitionDsl.Scope? = null,
isLazyInit: Boolean? = null,
isPrimary: Boolean? = null,
isAutowireCandidate: Boolean? = null,
initMethodName: String? = null,
destroyMethodName: String? = null,
description: String? = null,
role: BeanDefinitionDsl.Role? = null) {
bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) {
f.invoke(ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref())
}
}
/**
* Declare a bean definition using the given callable reference with 16 parameters
* autowired by type for obtaining a new instance.
*
* @param f the callable reference
* @param name the name of the bean
* @param scope Override the target scope of this bean, specifying a new scope name.
* @param isLazyInit Set whether this bean should be lazily initialized.
* @param isPrimary Set whether this bean is a primary autowire candidate.
* @param isAutowireCandidate Set whether this bean is a candidate for getting
* autowired into some other bean.
* @param initMethodName Set the name of the initializer method
* @param destroyMethodName Set the name of the destroy method
* @param description Set a human-readable description of this bean definition
* @param role Set the role hint for this bean definition
* @see GenericApplicationContext.registerBean
* @see org.springframework.beans.factory.config.BeanDefinition
* @since 5.2
*/
inline fun <reified T: Any, reified A: Any, reified B: Any, reified C: Any, reified D: Any, reified E: Any, reified F: Any,
reified G: Any, reified H: Any, reified I: Any, reified J: Any, reified K: Any, reified L: Any, reified M: Any,
reified N: Any, reified O: Any, reified P: Any>
bean(crossinline f: (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P) -> T,
name: String? = null,
scope: BeanDefinitionDsl.Scope? = null,
isLazyInit: Boolean? = null,
isPrimary: Boolean? = null,
isAutowireCandidate: Boolean? = null,
initMethodName: String? = null,
destroyMethodName: String? = null,
description: String? = null,
role: BeanDefinitionDsl.Role? = null) {
bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) {
f.invoke(ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref())
}
}
/**
* Declare a bean definition using the given callable reference with 17 parameters
* autowired by type for obtaining a new instance.
*
* @param f the callable reference
* @param name the name of the bean
* @param scope Override the target scope of this bean, specifying a new scope name.
* @param isLazyInit Set whether this bean should be lazily initialized.
* @param isPrimary Set whether this bean is a primary autowire candidate.
* @param isAutowireCandidate Set whether this bean is a candidate for getting
* autowired into some other bean.
* @param initMethodName Set the name of the initializer method
* @param destroyMethodName Set the name of the destroy method
* @param description Set a human-readable description of this bean definition
* @param role Set the role hint for this bean definition
* @see GenericApplicationContext.registerBean
* @see org.springframework.beans.factory.config.BeanDefinition
* @since 5.2
*/
inline fun <reified T: Any, reified A: Any, reified B: Any, reified C: Any, reified D: Any, reified E: Any, reified F: Any,
reified G: Any, reified H: Any, reified I: Any, reified J: Any, reified K: Any, reified L: Any, reified M: Any,
reified N: Any, reified O: Any, reified P: Any, reified Q: Any>
bean(crossinline f: (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q) -> T,
name: String? = null,
scope: BeanDefinitionDsl.Scope? = null,
isLazyInit: Boolean? = null,
isPrimary: Boolean? = null,
isAutowireCandidate: Boolean? = null,
initMethodName: String? = null,
destroyMethodName: String? = null,
description: String? = null,
role: BeanDefinitionDsl.Role? = null) {
bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) {
f.invoke(ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref())
}
}
/**
* Declare a bean definition using the given callable reference with 18 parameters
* autowired by type for obtaining a new instance.
*
* @param f the callable reference
* @param name the name of the bean
* @param scope Override the target scope of this bean, specifying a new scope name.
* @param isLazyInit Set whether this bean should be lazily initialized.
* @param isPrimary Set whether this bean is a primary autowire candidate.
* @param isAutowireCandidate Set whether this bean is a candidate for getting
* autowired into some other bean.
* @param initMethodName Set the name of the initializer method
* @param destroyMethodName Set the name of the destroy method
* @param description Set a human-readable description of this bean definition
* @param role Set the role hint for this bean definition
* @see GenericApplicationContext.registerBean
* @see org.springframework.beans.factory.config.BeanDefinition
* @since 5.2
*/
inline fun <reified T: Any, reified A: Any, reified B: Any, reified C: Any, reified D: Any, reified E: Any, reified F: Any,
reified G: Any, reified H: Any, reified I: Any, reified J: Any, reified K: Any, reified L: Any, reified M: Any,
reified N: Any, reified O: Any, reified P: Any, reified Q: Any, reified R: Any>
bean(crossinline f: (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R) -> T,
name: String? = null,
scope: BeanDefinitionDsl.Scope? = null,
isLazyInit: Boolean? = null,
isPrimary: Boolean? = null,
isAutowireCandidate: Boolean? = null,
initMethodName: String? = null,
destroyMethodName: String? = null,
description: String? = null,
role: BeanDefinitionDsl.Role? = null) {
bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) {
f.invoke(ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref())
}
}
/**
* Declare a bean definition using the given callable reference with 19 parameters
* autowired by type for obtaining a new instance.
*
* @param f the callable reference
* @param name the name of the bean
* @param scope Override the target scope of this bean, specifying a new scope name.
* @param isLazyInit Set whether this bean should be lazily initialized.
* @param isPrimary Set whether this bean is a primary autowire candidate.
* @param isAutowireCandidate Set whether this bean is a candidate for getting
* autowired into some other bean.
* @param initMethodName Set the name of the initializer method
* @param destroyMethodName Set the name of the destroy method
* @param description Set a human-readable description of this bean definition
* @param role Set the role hint for this bean definition
* @see GenericApplicationContext.registerBean
* @see org.springframework.beans.factory.config.BeanDefinition
* @since 5.2
*/
inline fun <reified T: Any, reified A: Any, reified B: Any, reified C: Any, reified D: Any, reified E: Any, reified F: Any,
reified G: Any, reified H: Any, reified I: Any, reified J: Any, reified K: Any, reified L: Any, reified M: Any,
reified N: Any, reified O: Any, reified P: Any, reified Q: Any, reified R: Any, reified S: Any>
bean(crossinline f: (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S) -> T,
name: String? = null,
scope: BeanDefinitionDsl.Scope? = null,
isLazyInit: Boolean? = null,
isPrimary: Boolean? = null,
isAutowireCandidate: Boolean? = null,
initMethodName: String? = null,
destroyMethodName: String? = null,
description: String? = null,
role: BeanDefinitionDsl.Role? = null) {
bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) {
f.invoke(ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref())
}
}
/**
* Declare a bean definition using the given callable reference with 20 parameters
* autowired by type for obtaining a new instance.
*
* @param f the callable reference
* @param name the name of the bean
* @param scope Override the target scope of this bean, specifying a new scope name.
* @param isLazyInit Set whether this bean should be lazily initialized.
* @param isPrimary Set whether this bean is a primary autowire candidate.
* @param isAutowireCandidate Set whether this bean is a candidate for getting
* autowired into some other bean.
* @param initMethodName Set the name of the initializer method
* @param destroyMethodName Set the name of the destroy method
* @param description Set a human-readable description of this bean definition
* @param role Set the role hint for this bean definition
* @see GenericApplicationContext.registerBean
* @see org.springframework.beans.factory.config.BeanDefinition
* @since 5.2
*/
inline fun <reified T: Any, reified A: Any, reified B: Any, reified C: Any, reified D: Any, reified E: Any, reified F: Any,
reified G: Any, reified H: Any, reified I: Any, reified J: Any, reified K: Any, reified L: Any, reified M: Any,
reified N: Any, reified O: Any, reified P: Any, reified Q: Any, reified R: Any, reified S: Any, reified U: Any>
bean(crossinline f: (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, U) -> T,
name: String? = null,
scope: BeanDefinitionDsl.Scope? = null,
isLazyInit: Boolean? = null,
isPrimary: Boolean? = null,
isAutowireCandidate: Boolean? = null,
initMethodName: String? = null,
destroyMethodName: String? = null,
description: String? = null,
role: BeanDefinitionDsl.Role? = null) {
bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) {
f.invoke(ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref())
}
}
/**
* Declare a bean definition using the given callable reference with 21 parameters
* autowired by type for obtaining a new instance.
*
* @param f the callable reference
* @param name the name of the bean
* @param scope Override the target scope of this bean, specifying a new scope name.
* @param isLazyInit Set whether this bean should be lazily initialized.
* @param isPrimary Set whether this bean is a primary autowire candidate.
* @param isAutowireCandidate Set whether this bean is a candidate for getting
* autowired into some other bean.
* @param initMethodName Set the name of the initializer method
* @param destroyMethodName Set the name of the destroy method
* @param description Set a human-readable description of this bean definition
* @param role Set the role hint for this bean definition
* @see GenericApplicationContext.registerBean
* @see org.springframework.beans.factory.config.BeanDefinition
* @since 5.2
*/
inline fun <reified T: Any, reified A: Any, reified B: Any, reified C: Any, reified D: Any, reified E: Any, reified F: Any,
reified G: Any, reified H: Any, reified I: Any, reified J: Any, reified K: Any, reified L: Any, reified M: Any,
reified N: Any, reified O: Any, reified P: Any, reified Q: Any, reified R: Any, reified S: Any, reified U: Any,
reified V: Any>
bean(crossinline f: (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, U, V) -> T,
name: String? = null,
scope: BeanDefinitionDsl.Scope? = null,
isLazyInit: Boolean? = null,
isPrimary: Boolean? = null,
isAutowireCandidate: Boolean? = null,
initMethodName: String? = null,
destroyMethodName: String? = null,
description: String? = null,
role: BeanDefinitionDsl.Role? = null) {
bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) {
f.invoke(ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref())
}
}
/**
* Declare a bean definition using the given callable reference with 22 parameters
* autowired by type for obtaining a new instance.
*
* @param f the callable reference
* @param name the name of the bean
* @param scope Override the target scope of this bean, specifying a new scope name.
* @param isLazyInit Set whether this bean should be lazily initialized.
* @param isPrimary Set whether this bean is a primary autowire candidate.
* @param isAutowireCandidate Set whether this bean is a candidate for getting
* autowired into some other bean.
* @param initMethodName Set the name of the initializer method
* @param destroyMethodName Set the name of the destroy method
* @param description Set a human-readable description of this bean definition
* @param role Set the role hint for this bean definition
* @see GenericApplicationContext.registerBean
* @see org.springframework.beans.factory.config.BeanDefinition
* @since 5.2
*/
inline fun <reified T: Any, reified A: Any, reified B: Any, reified C: Any, reified D: Any, reified E: Any, reified F: Any,
reified G: Any, reified H: Any, reified I: Any, reified J: Any, reified K: Any, reified L: Any, reified M: Any,
reified N: Any, reified O: Any, reified P: Any, reified Q: Any, reified R: Any, reified S: Any, reified U: Any,
reified V: Any, reified W: Any>
bean(crossinline f: (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, U, V, W) -> T,
name: String? = null,
scope: BeanDefinitionDsl.Scope? = null,
isLazyInit: Boolean? = null,
isPrimary: Boolean? = null,
isAutowireCandidate: Boolean? = null,
initMethodName: String? = null,
destroyMethodName: String? = null,
description: String? = null,
role: BeanDefinitionDsl.Role? = null) {
bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) {
f.invoke(ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref())
}
}
/**
* Limit access to `ref()` and `provider()` to bean supplier lambdas.
* @since 5.2

View File

@@ -156,6 +156,19 @@ class BeanDefinitionDslTests {
}
context.getBean<Baz>()
}
@Test // gh-21845
fun `Declare beans leveraging callable reference`() {
val beans = beans {
bean<Bar>()
bean(::baz)
}
val context = GenericApplicationContext().apply {
beans.initialize(this)
refresh()
}
context.getBean<Baz>()
}
}
@@ -164,3 +177,5 @@ class Bar
class Baz(val bar: Bar)
class FooFoo(val name: String)
class BarBar(val foos: Collection<Foo>)
fun baz(bar: Bar) = Baz(bar)