Browse Source

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
pull/22705/head
Sebastien Deleuze 7 years ago
parent
commit
fb1da01410
  1. 819
      spring-context/src/main/kotlin/org/springframework/context/support/BeanDefinitionDsl.kt
  2. 15
      spring-context/src/test/kotlin/org/springframework/context/support/BeanDefinitionDslTests.kt
  3. 38
      src/docs/asciidoc/languages/kotlin.adoc

819
spring-context/src/main/kotlin/org/springframework/context/support/BeanDefinitionDsl.kt

@ -235,6 +235,825 @@ open class BeanDefinitionDsl(private val init: BeanDefinitionDsl.() -> Unit, @@ -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

15
spring-context/src/test/kotlin/org/springframework/context/support/BeanDefinitionDslTests.kt

@ -156,6 +156,19 @@ class BeanDefinitionDslTests { @@ -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 @@ -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)

38
src/docs/asciidoc/languages/kotlin.adoc

@ -180,10 +180,18 @@ In Java, you can, for example, write the following: @@ -180,10 +180,18 @@ In Java, you can, for example, write the following:
[source,java,indent=0]
----
class Foo {}
class Bar {
private final Foo foo;
public Bar(Foo foo) {
this.foo = foo;
}
}
GenericApplicationContext context = new GenericApplicationContext();
context.registerBean(Foo.class);
context.registerBean(Bar.class, () -> new Bar(context.getBean(Foo.class))
);
context.registerBean(Bar.class, () -> new Bar(context.getBean(Foo.class)));
----
In Kotlin, with reified type parameters and `GenericApplicationContext` Kotlin extensions,
@ -191,6 +199,10 @@ you can instead write the following: @@ -191,6 +199,10 @@ you can instead write the following:
[source,kotlin,indent=0]
----
class Foo
class Bar(private val foo: Foo)
val context = GenericApplicationContext().apply {
registerBean<Foo>()
registerBean { Bar(it.getBean()) }
@ -198,7 +210,7 @@ you can instead write the following: @@ -198,7 +210,7 @@ you can instead write the following:
----
====
If the class `Bar` has a single constructor, you can even just specify the bean class,
When the class `Bar` has a single constructor, you can even just specify the bean class,
the constructor parameters will be autowired by type:
====
@ -214,10 +226,21 @@ In order to allow a more declarative approach and cleaner syntax, Spring Framewo @@ -214,10 +226,21 @@ In order to allow a more declarative approach and cleaner syntax, Spring Framewo
a {doc-root}/spring-framework/docs/{spring-version}/kdoc-api/spring-framework/org.springframework.context.support/-bean-definition-dsl/[Kotlin bean definition DSL]
It declares an `ApplicationContextInitializer` through a clean declarative API,
which lets you deal with profiles and `Environment` for customizing
how beans are registered. The following example creates a `Play` profile:
how beans are registered.
In the following example notice that:
- Type inference usually allows to avoid specifying the type for bean references like `ref("bazBean")`
- It is possible to use Kotlin top level functions to declare beans using callable references like `bean(::myRouter)` in this example
- When specifying `bean<Bar>()` or `bean(::myRouter)`, parameters are autowired by type
- The `FooBar` bean will be registered only if the `foobar` profile is active
[source,kotlin,indent=0]
----
class Foo
class Bar(private val foo: Foo)
class Baz(var message: String = "")
class FooBar(private val baz: Baz)
val myBeans = beans {
bean<Foo>()
bean<Bar>()
@ -229,6 +252,11 @@ how beans are registered. The following example creates a `Play` profile: @@ -229,6 +252,11 @@ how beans are registered. The following example creates a `Play` profile:
profile("foobar") {
bean { FooBar(ref("bazBean")) }
}
bean(::myRouter)
}
fun myRouter(foo: Foo, bar: Bar, baz: Baz) = router {
// ...
}
----
@ -275,7 +303,7 @@ and idiomatic Kotlin code to build a `RouterFunction` instance as the following @@ -275,7 +303,7 @@ and idiomatic Kotlin code to build a `RouterFunction` instance as the following
[source,kotlin,indent=0]
----
val routes: RouterFunction<ServerResponse> = router {
val myRouter: RouterFunction<ServerResponse> = router {
accept(TEXT_HTML).nest {
GET("/") { ok().render("index") }
GET("/sse") { ok().render("sse") }

Loading…
Cancel
Save