From aa9ab8e54581bb3a56cbd25f22b28fa0dd268c61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Deleuze?= Date: Mon, 19 May 2025 12:56:05 +0200 Subject: [PATCH] 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() registerBean(::myRouter) }) fun myRouter(myRepository: MyRepository) = router { ... } Closes gh-34922 --- .../beans/factory/BeanRegistrarDsl.kt | 688 ++++++++++++++++++ .../BeanRegistrarDslConfigurationTests.kt | 5 + 2 files changed, 693 insertions(+) diff --git a/spring-beans/src/main/kotlin/org/springframework/beans/factory/BeanRegistrarDsl.kt b/spring-beans/src/main/kotlin/org/springframework/beans/factory/BeanRegistrarDsl.kt index 8daf689cf4b..49f713df8fd 100644 --- a/spring-beans/src/main/kotlin/org/springframework/beans/factory/BeanRegistrarDsl.kt +++ b/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) } + // 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 diff --git a/spring-context/src/test/kotlin/org/springframework/context/annotation/BeanRegistrarDslConfigurationTests.kt b/spring-context/src/test/kotlin/org/springframework/context/annotation/BeanRegistrarDslConfigurationTests.kt index a3558bca340..c8f59673feb 100644 --- a/spring-context/src/test/kotlin/org/springframework/context/annotation/BeanRegistrarDslConfigurationTests.kt +++ b/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.isLazyInit).isTrue() assertThat(beanDefinition.description).isEqualTo("Custom description") + assertThat(context.getBean()).isEqualTo(Boo("booFactory")) } @Test @@ -75,6 +76,7 @@ class BeanRegistrarDslConfigurationTests { class Foo data class Bar(val foo: Foo) data class Baz(val message: String = "") + data class Boo(val message: String = "") class Init : InitializingBean { var initialized: Boolean = false @@ -102,6 +104,7 @@ class BeanRegistrarDslConfigurationTests { registerBean { Baz("Hello World!") } } registerBean() + registerBean(::booFactory, "fooFactory") }) @Configuration @@ -126,3 +129,5 @@ class BeanRegistrarDslConfigurationTests { register(SampleBeanRegistrar()) }) } + +fun booFactory() = BeanRegistrarDslConfigurationTests.Boo("booFactory")