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 a28328dd2a6..665ee6da2f1 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 @@ -334,6 +334,15 @@ open class BeanRegistrarDsl(private val init: BeanRegistrarDsl.() -> Unit): Bean return registry.registerBean(T::class.java, customizer) } + /** + * Register beans using the given [BeanRegistrar]. + * @param registrar the bean registrar that will be called to register + * additional beans + */ + fun register(registrar: BeanRegistrar) { + return registry.register(registrar) + } + /** * Apply the nested block if the given profile expression matches the * active profiles. 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 1ae9bd234b7..39faddebf32 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 @@ -65,6 +65,12 @@ class BeanRegistrarDslConfigurationTests { assertThat(beanDefinition.resolvableType.resolveGeneric(0)).isEqualTo(Foo::class.java) } + @Test + fun chainedBeanRegistrar() { + val context = AnnotationConfigApplicationContext(ChainedBeanRegistrarKotlinConfiguration::class.java) + assertThat(context.getBean().foo).isEqualTo(context.getBean()) + } + class Foo data class Bar(val foo: Foo) data class Baz(val message: String = "") @@ -109,4 +115,12 @@ class BeanRegistrarDslConfigurationTests { } } }) + + @Configuration + @Import(ChainedBeanRegistrar::class) + internal class ChainedBeanRegistrarKotlinConfiguration + + private class ChainedBeanRegistrar : BeanRegistrarDsl({ + register(SampleBeanRegistrar()) + }) }