Browse Source

Add support for callable references to BeanRegistrarDsl

BeanDefinitionDsl is allowing to create a bean from a callable
reference with its parameters autowired by type,
BeanRegistrarDsl should allow that too. For example:

class SampleBeanRegistrar : BeanRegistrarDsl({
    registerBean<MyRepository>()
    registerBean(::myRouter)
})

fun myRouter(myRepository: MyRepository) = router {
    ...
}

Closes gh-34922
pull/34925/head
Sébastien Deleuze 7 months ago
parent
commit
aa9ab8e545
  1. 688
      spring-beans/src/main/kotlin/org/springframework/beans/factory/BeanRegistrarDsl.kt
  2. 5
      spring-context/src/test/kotlin/org/springframework/context/annotation/BeanRegistrarDslConfigurationTests.kt

688
spring-beans/src/main/kotlin/org/springframework/beans/factory/BeanRegistrarDsl.kt

@ -380,6 +380,694 @@ open class BeanRegistrarDsl(private val init: BeanRegistrarDsl.() -> Unit): Bean
return registry.registerBean(T::class.java, customizer) return registry.registerBean(T::class.java, customizer)
} }
// Function with 0 parameter
/**
* Register a bean of type [T] which will be instantiated by invoking the
* provided [function][f].
* @param T the bean type
* @param autowirable set whether this bean is a candidate for getting
* autowired into some other bean
* @param backgroundInit set whether this bean allows for instantiation
* on a background thread
* @param description a human-readable description of this bean
* @param fallback set whether this bean is a fallback autowire candidate
* @param infrastructure set whether this bean has an infrastructure role,
* meaning it has no relevance to the end-user
* @param lazyInit set whether this bean is lazily initialized
* @param order the sort order of this bean
* @param primary set whether this bean is a primary autowire candidate
* @param prototype set whether this bean has a prototype scope
*/
inline fun <reified T : Any> registerBean(
crossinline f: () -> T,
autowirable: Boolean = true,
backgroundInit: Boolean = false,
description: String? = null,
fallback: Boolean = false,
infrastructure: Boolean = false,
lazyInit: Boolean = false,
order: Int? = null,
primary: Boolean = false,
prototype: Boolean = false) =
registerBean(autowirable, backgroundInit, description, fallback, infrastructure, lazyInit, order, primary, prototype) {
f.invoke()
}
/**
* Register a bean of type [T] which will be instantiated by invoking the
* provided [function][f].
* @param T the bean type
* @param name the name of the bean
* @param autowirable set whether this bean is a candidate for getting
* autowired into some other bean
* @param backgroundInit set whether this bean allows for instantiation
* on a background thread
* @param description a human-readable description of this bean
* @param fallback set whether this bean is a fallback autowire candidate
* @param infrastructure set whether this bean has an infrastructure role,
* meaning it has no relevance to the end-user
* @param lazyInit set whether this bean is lazily initialized
* @param order the sort order of this bean
* @param primary set whether this bean is a primary autowire candidate
* @param prototype set whether this bean has a prototype scope
*/
inline fun <reified T : Any> registerBean(
crossinline f: () -> T,
name: String,
autowirable: Boolean = true,
backgroundInit: Boolean = false,
description: String? = null,
fallback: Boolean = false,
infrastructure: Boolean = false,
lazyInit: Boolean = false,
order: Int? = null,
primary: Boolean = false,
prototype: Boolean = false) =
registerBean(name, autowirable, backgroundInit, description, fallback, infrastructure, lazyInit, order, primary, prototype) {
f.invoke()
}
// Function with 1 parameter
/**
* Register a bean of type [T] which will be instantiated by invoking the
* provided [function][f] with its parameters autowired by type.
* @param T the bean type
* @param autowirable set whether this bean is a candidate for getting
* autowired into some other bean
* @param backgroundInit set whether this bean allows for instantiation
* on a background thread
* @param description a human-readable description of this bean
* @param fallback set whether this bean is a fallback autowire candidate
* @param infrastructure set whether this bean has an infrastructure role,
* meaning it has no relevance to the end-user
* @param lazyInit set whether this bean is lazily initialized
* @param order the sort order of this bean
* @param primary set whether this bean is a primary autowire candidate
* @param prototype set whether this bean has a prototype scope
*/
inline fun <reified T : Any, reified A: Any> registerBean(
crossinline f: (A) -> T,
autowirable: Boolean = true,
backgroundInit: Boolean = false,
description: String? = null,
fallback: Boolean = false,
infrastructure: Boolean = false,
lazyInit: Boolean = false,
order: Int? = null,
primary: Boolean = false,
prototype: Boolean = false) =
registerBean(autowirable, backgroundInit, description, fallback, infrastructure, lazyInit, order, primary, prototype) {
f.invoke(bean())
}
/**
* Register a bean of type [T] which will be instantiated by invoking the
* provided [function][f] with its parameters autowired by type.
* @param T the bean type
* @param name the name of the bean
* @param autowirable set whether this bean is a candidate for getting
* autowired into some other bean
* @param backgroundInit set whether this bean allows for instantiation
* on a background thread
* @param description a human-readable description of this bean
* @param fallback set whether this bean is a fallback autowire candidate
* @param infrastructure set whether this bean has an infrastructure role,
* meaning it has no relevance to the end-user
* @param lazyInit set whether this bean is lazily initialized
* @param order the sort order of this bean
* @param primary set whether this bean is a primary autowire candidate
* @param prototype set whether this bean has a prototype scope
*/
inline fun <reified T : Any, reified A: Any> registerBean(
crossinline f: (A) -> T,
name: String,
autowirable: Boolean = true,
backgroundInit: Boolean = false,
description: String? = null,
fallback: Boolean = false,
infrastructure: Boolean = false,
lazyInit: Boolean = false,
order: Int? = null,
primary: Boolean = false,
prototype: Boolean = false) =
registerBean(name, autowirable, backgroundInit, description, fallback, infrastructure, lazyInit, order, primary, prototype) {
f.invoke(bean())
}
// Function with 2 parameters
/**
* Register a bean of type [T] which will be instantiated by invoking the
* provided [function][f] with its parameters autowired by type.
* @param T the bean type
* @param autowirable set whether this bean is a candidate for getting
* autowired into some other bean
* @param backgroundInit set whether this bean allows for instantiation
* on a background thread
* @param description a human-readable description of this bean
* @param fallback set whether this bean is a fallback autowire candidate
* @param infrastructure set whether this bean has an infrastructure role,
* meaning it has no relevance to the end-user
* @param lazyInit set whether this bean is lazily initialized
* @param order the sort order of this bean
* @param primary set whether this bean is a primary autowire candidate
* @param prototype set whether this bean has a prototype scope
*/
inline fun <reified T : Any, reified A: Any, reified B: Any> registerBean(
crossinline f: (A, B) -> T,
autowirable: Boolean = true,
backgroundInit: Boolean = false,
description: String? = null,
fallback: Boolean = false,
infrastructure: Boolean = false,
lazyInit: Boolean = false,
order: Int? = null,
primary: Boolean = false,
prototype: Boolean = false) =
registerBean(autowirable, backgroundInit, description, fallback, infrastructure, lazyInit, order, primary, prototype) {
f.invoke(bean(), bean())
}
/**
* Register a bean of type [T] which will be instantiated by invoking the
* provided [function][f] with its parameters autowired by type.
* @param T the bean type
* @param name the name of the bean
* @param autowirable set whether this bean is a candidate for getting
* autowired into some other bean
* @param backgroundInit set whether this bean allows for instantiation
* on a background thread
* @param description a human-readable description of this bean
* @param fallback set whether this bean is a fallback autowire candidate
* @param infrastructure set whether this bean has an infrastructure role,
* meaning it has no relevance to the end-user
* @param lazyInit set whether this bean is lazily initialized
* @param order the sort order of this bean
* @param primary set whether this bean is a primary autowire candidate
* @param prototype set whether this bean has a prototype scope
*/
inline fun <reified T : Any, reified A: Any, reified B: Any> registerBean(
crossinline f: (A, B) -> T,
name: String,
autowirable: Boolean = true,
backgroundInit: Boolean = false,
description: String? = null,
fallback: Boolean = false,
infrastructure: Boolean = false,
lazyInit: Boolean = false,
order: Int? = null,
primary: Boolean = false,
prototype: Boolean = false) =
registerBean(name, autowirable, backgroundInit, description, fallback, infrastructure, lazyInit, order, primary, prototype) {
f.invoke(bean(), bean())
}
// Function with 3 parameters
/**
* Register a bean of type [T] which will be instantiated by invoking the
* provided [function][f] with its parameters autowired by type.
* @param T the bean type
* @param autowirable set whether this bean is a candidate for getting
* autowired into some other bean
* @param backgroundInit set whether this bean allows for instantiation
* on a background thread
* @param description a human-readable description of this bean
* @param fallback set whether this bean is a fallback autowire candidate
* @param infrastructure set whether this bean has an infrastructure role,
* meaning it has no relevance to the end-user
* @param lazyInit set whether this bean is lazily initialized
* @param order the sort order of this bean
* @param primary set whether this bean is a primary autowire candidate
* @param prototype set whether this bean has a prototype scope
*/
inline fun <reified T : Any, reified A: Any, reified B: Any, reified C: Any> registerBean(
crossinline f: (A, B, C) -> T,
autowirable: Boolean = true,
backgroundInit: Boolean = false,
description: String? = null,
fallback: Boolean = false,
infrastructure: Boolean = false,
lazyInit: Boolean = false,
order: Int? = null,
primary: Boolean = false,
prototype: Boolean = false) =
registerBean(autowirable, backgroundInit, description, fallback, infrastructure, lazyInit, order, primary, prototype) {
f.invoke(bean(), bean(), bean())
}
/**
* Register a bean of type [T] which will be instantiated by invoking the
* provided [function][f] with its parameters autowired by type.
* @param T the bean type
* @param name the name of the bean
* @param autowirable set whether this bean is a candidate for getting
* autowired into some other bean
* @param backgroundInit set whether this bean allows for instantiation
* on a background thread
* @param description a human-readable description of this bean
* @param fallback set whether this bean is a fallback autowire candidate
* @param infrastructure set whether this bean has an infrastructure role,
* meaning it has no relevance to the end-user
* @param lazyInit set whether this bean is lazily initialized
* @param order the sort order of this bean
* @param primary set whether this bean is a primary autowire candidate
* @param prototype set whether this bean has a prototype scope
*/
inline fun <reified T : Any, reified A: Any, reified B: Any, reified C: Any> registerBean(
crossinline f: (A, B, C) -> T,
name: String,
autowirable: Boolean = true,
backgroundInit: Boolean = false,
description: String? = null,
fallback: Boolean = false,
infrastructure: Boolean = false,
lazyInit: Boolean = false,
order: Int? = null,
primary: Boolean = false,
prototype: Boolean = false) =
registerBean(name, autowirable, backgroundInit, description, fallback, infrastructure, lazyInit, order, primary, prototype) {
f.invoke(bean(), bean(), bean())
}
// Function with 4 parameters
/**
* Register a bean of type [T] which will be instantiated by invoking the
* provided [function][f] with its parameters autowired by type.
* @param T the bean type
* @param autowirable set whether this bean is a candidate for getting
* autowired into some other bean
* @param backgroundInit set whether this bean allows for instantiation
* on a background thread
* @param description a human-readable description of this bean
* @param fallback set whether this bean is a fallback autowire candidate
* @param infrastructure set whether this bean has an infrastructure role,
* meaning it has no relevance to the end-user
* @param lazyInit set whether this bean is lazily initialized
* @param order the sort order of this bean
* @param primary set whether this bean is a primary autowire candidate
* @param prototype set whether this bean has a prototype scope
*/
inline fun <reified T : Any, reified A: Any, reified B: Any, reified C: Any, reified D: Any> registerBean(
crossinline f: (A, B, C, D) -> T,
autowirable: Boolean = true,
backgroundInit: Boolean = false,
description: String? = null,
fallback: Boolean = false,
infrastructure: Boolean = false,
lazyInit: Boolean = false,
order: Int? = null,
primary: Boolean = false,
prototype: Boolean = false) =
registerBean(autowirable, backgroundInit, description, fallback, infrastructure, lazyInit, order, primary, prototype) {
f.invoke(bean(), bean(), bean(), bean())
}
/**
* Register a bean of type [T] which will be instantiated by invoking the
* provided [function][f] with its parameters autowired by type.
* @param T the bean type
* @param name the name of the bean
* @param autowirable set whether this bean is a candidate for getting
* autowired into some other bean
* @param backgroundInit set whether this bean allows for instantiation
* on a background thread
* @param description a human-readable description of this bean
* @param fallback set whether this bean is a fallback autowire candidate
* @param infrastructure set whether this bean has an infrastructure role,
* meaning it has no relevance to the end-user
* @param lazyInit set whether this bean is lazily initialized
* @param order the sort order of this bean
* @param primary set whether this bean is a primary autowire candidate
* @param prototype set whether this bean has a prototype scope
*/
inline fun <reified T : Any, reified A: Any, reified B: Any, reified C: Any, reified D: Any> registerBean(
crossinline f: (A, B, C, D) -> T,
name: String,
autowirable: Boolean = true,
backgroundInit: Boolean = false,
description: String? = null,
fallback: Boolean = false,
infrastructure: Boolean = false,
lazyInit: Boolean = false,
order: Int? = null,
primary: Boolean = false,
prototype: Boolean = false) =
registerBean(name, autowirable, backgroundInit, description, fallback, infrastructure, lazyInit, order, primary, prototype) {
f.invoke(bean(), bean(), bean(), bean())
}
// Function with 5 parameters
/**
* Register a bean of type [T] which will be instantiated by invoking the
* provided [function][f] with its parameters autowired by type.
* @param T the bean type
* @param autowirable set whether this bean is a candidate for getting
* autowired into some other bean
* @param backgroundInit set whether this bean allows for instantiation
* on a background thread
* @param description a human-readable description of this bean
* @param fallback set whether this bean is a fallback autowire candidate
* @param infrastructure set whether this bean has an infrastructure role,
* meaning it has no relevance to the end-user
* @param lazyInit set whether this bean is lazily initialized
* @param order the sort order of this bean
* @param primary set whether this bean is a primary autowire candidate
* @param prototype set whether this bean has a prototype scope
*/
inline fun <reified T : Any, reified A: Any, reified B: Any, reified C: Any, reified D: Any, reified E: Any> registerBean(
crossinline f: (A, B, C, D, E) -> T,
autowirable: Boolean = true,
backgroundInit: Boolean = false,
description: String? = null,
fallback: Boolean = false,
infrastructure: Boolean = false,
lazyInit: Boolean = false,
order: Int? = null,
primary: Boolean = false,
prototype: Boolean = false) =
registerBean(autowirable, backgroundInit, description, fallback, infrastructure, lazyInit, order, primary, prototype) {
f.invoke(bean(), bean(), bean(), bean(), bean())
}
/**
* Register a bean of type [T] which will be instantiated by invoking the
* provided [function][f] with its parameters autowired by type.
* @param T the bean type
* @param name the name of the bean
* @param autowirable set whether this bean is a candidate for getting
* autowired into some other bean
* @param backgroundInit set whether this bean allows for instantiation
* on a background thread
* @param description a human-readable description of this bean
* @param fallback set whether this bean is a fallback autowire candidate
* @param infrastructure set whether this bean has an infrastructure role,
* meaning it has no relevance to the end-user
* @param lazyInit set whether this bean is lazily initialized
* @param order the sort order of this bean
* @param primary set whether this bean is a primary autowire candidate
* @param prototype set whether this bean has a prototype scope
*/
inline fun <reified T : Any, reified A: Any, reified B: Any, reified C: Any, reified D: Any, reified E: Any> registerBean(
crossinline f: (A, B, C, D, E) -> T,
name: String,
autowirable: Boolean = true,
backgroundInit: Boolean = false,
description: String? = null,
fallback: Boolean = false,
infrastructure: Boolean = false,
lazyInit: Boolean = false,
order: Int? = null,
primary: Boolean = false,
prototype: Boolean = false) =
registerBean(name, autowirable, backgroundInit, description, fallback, infrastructure, lazyInit, order, primary, prototype) {
f.invoke(bean(), bean(), bean(), bean(), bean())
}
// Function with 6 parameters
/**
* Register a bean of type [T] which will be instantiated by invoking the
* provided [function][f] with its parameters autowired by type.
* @param T the bean type
* @param autowirable set whether this bean is a candidate for getting
* autowired into some other bean
* @param backgroundInit set whether this bean allows for instantiation
* on a background thread
* @param description a human-readable description of this bean
* @param fallback set whether this bean is a fallback autowire candidate
* @param infrastructure set whether this bean has an infrastructure role,
* meaning it has no relevance to the end-user
* @param lazyInit set whether this bean is lazily initialized
* @param order the sort order of this bean
* @param primary set whether this bean is a primary autowire candidate
* @param prototype set whether this bean has a prototype scope
*/
inline fun <reified T : Any, reified A: Any, reified B: Any, reified C: Any, reified D: Any, reified E: Any,
reified F: Any> registerBean(
crossinline f: (A, B, C, D, E, F) -> T,
autowirable: Boolean = true,
backgroundInit: Boolean = false,
description: String? = null,
fallback: Boolean = false,
infrastructure: Boolean = false,
lazyInit: Boolean = false,
order: Int? = null,
primary: Boolean = false,
prototype: Boolean = false) =
registerBean(autowirable, backgroundInit, description, fallback, infrastructure, lazyInit, order, primary, prototype) {
f.invoke(bean(), bean(), bean(), bean(), bean(), bean())
}
/**
* Register a bean of type [T] which will be instantiated by invoking the
* provided [function][f] with its parameters autowired by type.
* @param T the bean type
* @param name the name of the bean
* @param autowirable set whether this bean is a candidate for getting
* autowired into some other bean
* @param backgroundInit set whether this bean allows for instantiation
* on a background thread
* @param description a human-readable description of this bean
* @param fallback set whether this bean is a fallback autowire candidate
* @param infrastructure set whether this bean has an infrastructure role,
* meaning it has no relevance to the end-user
* @param lazyInit set whether this bean is lazily initialized
* @param order the sort order of this bean
* @param primary set whether this bean is a primary autowire candidate
* @param prototype set whether this bean has a prototype scope
*/
inline fun <reified T : Any, reified A: Any, reified B: Any, reified C: Any, reified D: Any, reified E: Any,
reified F: Any> registerBean(
crossinline f: (A, B, C, D, E, F) -> T,
name: String,
autowirable: Boolean = true,
backgroundInit: Boolean = false,
description: String? = null,
fallback: Boolean = false,
infrastructure: Boolean = false,
lazyInit: Boolean = false,
order: Int? = null,
primary: Boolean = false,
prototype: Boolean = false) =
registerBean(name, autowirable, backgroundInit, description, fallback, infrastructure, lazyInit, order, primary, prototype) {
f.invoke(bean(), bean(), bean(), bean(), bean(), bean())
}
// Function with 7 parameters
/**
* Register a bean of type [T] which will be instantiated by invoking the
* provided [function][f] with its parameters autowired by type.
* @param T the bean type
* @param autowirable set whether this bean is a candidate for getting
* autowired into some other bean
* @param backgroundInit set whether this bean allows for instantiation
* on a background thread
* @param description a human-readable description of this bean
* @param fallback set whether this bean is a fallback autowire candidate
* @param infrastructure set whether this bean has an infrastructure role,
* meaning it has no relevance to the end-user
* @param lazyInit set whether this bean is lazily initialized
* @param order the sort order of this bean
* @param primary set whether this bean is a primary autowire candidate
* @param prototype set whether this bean has a prototype scope
*/
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> registerBean(
crossinline f: (A, B, C, D, E, F, G) -> T,
autowirable: Boolean = true,
backgroundInit: Boolean = false,
description: String? = null,
fallback: Boolean = false,
infrastructure: Boolean = false,
lazyInit: Boolean = false,
order: Int? = null,
primary: Boolean = false,
prototype: Boolean = false) =
registerBean(autowirable, backgroundInit, description, fallback, infrastructure, lazyInit, order, primary, prototype) {
f.invoke(bean(), bean(), bean(), bean(), bean(), bean(), bean())
}
/**
* Register a bean of type [T] which will be instantiated by invoking the
* provided [function][f] with its parameters autowired by type.
* @param T the bean type
* @param name the name of the bean
* @param autowirable set whether this bean is a candidate for getting
* autowired into some other bean
* @param backgroundInit set whether this bean allows for instantiation
* on a background thread
* @param description a human-readable description of this bean
* @param fallback set whether this bean is a fallback autowire candidate
* @param infrastructure set whether this bean has an infrastructure role,
* meaning it has no relevance to the end-user
* @param lazyInit set whether this bean is lazily initialized
* @param order the sort order of this bean
* @param primary set whether this bean is a primary autowire candidate
* @param prototype set whether this bean has a prototype scope
*/
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> registerBean(
crossinline f: (A, B, C, D, E, F, G) -> T,
name: String,
autowirable: Boolean = true,
backgroundInit: Boolean = false,
description: String? = null,
fallback: Boolean = false,
infrastructure: Boolean = false,
lazyInit: Boolean = false,
order: Int? = null,
primary: Boolean = false,
prototype: Boolean = false) =
registerBean(name, autowirable, backgroundInit, description, fallback, infrastructure, lazyInit, order, primary, prototype) {
f.invoke(bean(), bean(), bean(), bean(), bean(), bean(), bean())
}
// Function with 8 parameters
/**
* Register a bean of type [T] which will be instantiated by invoking the
* provided [function][f] with its parameters autowired by type.
* @param T the bean type
* @param autowirable set whether this bean is a candidate for getting
* autowired into some other bean
* @param backgroundInit set whether this bean allows for instantiation
* on a background thread
* @param description a human-readable description of this bean
* @param fallback set whether this bean is a fallback autowire candidate
* @param infrastructure set whether this bean has an infrastructure role,
* meaning it has no relevance to the end-user
* @param lazyInit set whether this bean is lazily initialized
* @param order the sort order of this bean
* @param primary set whether this bean is a primary autowire candidate
* @param prototype set whether this bean has a prototype scope
*/
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> registerBean(
crossinline f: (A, B, C, D, E, F, G, H) -> T,
autowirable: Boolean = true,
backgroundInit: Boolean = false,
description: String? = null,
fallback: Boolean = false,
infrastructure: Boolean = false,
lazyInit: Boolean = false,
order: Int? = null,
primary: Boolean = false,
prototype: Boolean = false) =
registerBean(autowirable, backgroundInit, description, fallback, infrastructure, lazyInit, order, primary, prototype) {
f.invoke(bean(), bean(), bean(), bean(), bean(), bean(), bean(), bean())
}
/**
* Register a bean of type [T] which will be instantiated by invoking the
* provided [function][f] with its parameters autowired by type.
* @param T the bean type
* @param name the name of the bean
* @param autowirable set whether this bean is a candidate for getting
* autowired into some other bean
* @param backgroundInit set whether this bean allows for instantiation
* on a background thread
* @param description a human-readable description of this bean
* @param fallback set whether this bean is a fallback autowire candidate
* @param infrastructure set whether this bean has an infrastructure role,
* meaning it has no relevance to the end-user
* @param lazyInit set whether this bean is lazily initialized
* @param order the sort order of this bean
* @param primary set whether this bean is a primary autowire candidate
* @param prototype set whether this bean has a prototype scope
*/
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> registerBean(
crossinline f: (A, B, C, D, E, F, G, H) -> T,
name: String,
autowirable: Boolean = true,
backgroundInit: Boolean = false,
description: String? = null,
fallback: Boolean = false,
infrastructure: Boolean = false,
lazyInit: Boolean = false,
order: Int? = null,
primary: Boolean = false,
prototype: Boolean = false) =
registerBean(name, autowirable, backgroundInit, description, fallback, infrastructure, lazyInit, order, primary, prototype) {
f.invoke(bean(), bean(), bean(), bean(), bean(), bean(), bean(), bean())
}
// Function with 9 parameters
/**
* Register a bean of type [T] which will be instantiated by invoking the
* provided [function][f] with its parameters autowired by type.
* @param T the bean type
* @param autowirable set whether this bean is a candidate for getting
* autowired into some other bean
* @param backgroundInit set whether this bean allows for instantiation
* on a background thread
* @param description a human-readable description of this bean
* @param fallback set whether this bean is a fallback autowire candidate
* @param infrastructure set whether this bean has an infrastructure role,
* meaning it has no relevance to the end-user
* @param lazyInit set whether this bean is lazily initialized
* @param order the sort order of this bean
* @param primary set whether this bean is a primary autowire candidate
* @param prototype set whether this bean has a prototype scope
*/
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> registerBean(
crossinline f: (A, B, C, D, E, F, G, H, I) -> T,
autowirable: Boolean = true,
backgroundInit: Boolean = false,
description: String? = null,
fallback: Boolean = false,
infrastructure: Boolean = false,
lazyInit: Boolean = false,
order: Int? = null,
primary: Boolean = false,
prototype: Boolean = false) =
registerBean(autowirable, backgroundInit, description, fallback, infrastructure, lazyInit, order, primary, prototype) {
f.invoke(bean(), bean(), bean(), bean(), bean(), bean(), bean(), bean(), bean())
}
/**
* Register a bean of type [T] which will be instantiated by invoking the
* provided [function][f] with its parameters autowired by type.
* @param T the bean type
* @param name the name of the bean
* @param autowirable set whether this bean is a candidate for getting
* autowired into some other bean
* @param backgroundInit set whether this bean allows for instantiation
* on a background thread
* @param description a human-readable description of this bean
* @param fallback set whether this bean is a fallback autowire candidate
* @param infrastructure set whether this bean has an infrastructure role,
* meaning it has no relevance to the end-user
* @param lazyInit set whether this bean is lazily initialized
* @param order the sort order of this bean
* @param primary set whether this bean is a primary autowire candidate
* @param prototype set whether this bean has a prototype scope
*/
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> registerBean(
crossinline f: (A, B, C, D, E, F, G, H, I) -> T,
name: String,
autowirable: Boolean = true,
backgroundInit: Boolean = false,
description: String? = null,
fallback: Boolean = false,
infrastructure: Boolean = false,
lazyInit: Boolean = false,
order: Int? = null,
primary: Boolean = false,
prototype: Boolean = false) =
registerBean(name, autowirable, backgroundInit, description, fallback, infrastructure, lazyInit, order, primary, prototype) {
f.invoke(bean(), bean(), bean(), bean(), bean(), bean(), bean(), bean(), bean())
}
/** /**
* Context available from the bean instance supplier designed to give access * Context available from the bean instance supplier designed to give access

5
spring-context/src/test/kotlin/org/springframework/context/annotation/BeanRegistrarDslConfigurationTests.kt

@ -46,6 +46,7 @@ class BeanRegistrarDslConfigurationTests {
assertThat(beanDefinition.scope).isEqualTo(BeanDefinition.SCOPE_PROTOTYPE) assertThat(beanDefinition.scope).isEqualTo(BeanDefinition.SCOPE_PROTOTYPE)
assertThat(beanDefinition.isLazyInit).isTrue() assertThat(beanDefinition.isLazyInit).isTrue()
assertThat(beanDefinition.description).isEqualTo("Custom description") assertThat(beanDefinition.description).isEqualTo("Custom description")
assertThat(context.getBean<Boo>()).isEqualTo(Boo("booFactory"))
} }
@Test @Test
@ -75,6 +76,7 @@ class BeanRegistrarDslConfigurationTests {
class Foo class Foo
data class Bar(val foo: Foo) data class Bar(val foo: Foo)
data class Baz(val message: String = "") data class Baz(val message: String = "")
data class Boo(val message: String = "")
class Init : InitializingBean { class Init : InitializingBean {
var initialized: Boolean = false var initialized: Boolean = false
@ -102,6 +104,7 @@ class BeanRegistrarDslConfigurationTests {
registerBean { Baz("Hello World!") } registerBean { Baz("Hello World!") }
} }
registerBean<Init>() registerBean<Init>()
registerBean(::booFactory, "fooFactory")
}) })
@Configuration @Configuration
@ -126,3 +129,5 @@ class BeanRegistrarDslConfigurationTests {
register(SampleBeanRegistrar()) register(SampleBeanRegistrar())
}) })
} }
fun booFactory() = BeanRegistrarDslConfigurationTests.Boo("booFactory")

Loading…
Cancel
Save