Skip to content

Commit 762831e

Browse files
committed
Add BeanRegistrarDsl.register
See gh-34557
1 parent 641b809 commit 762831e

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

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

+9
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,15 @@ open class BeanRegistrarDsl(private val init: BeanRegistrarDsl.() -> Unit): Bean
334334
return registry.registerBean(T::class.java, customizer)
335335
}
336336

337+
/**
338+
* Register beans using the given [BeanRegistrar].
339+
* @param registrar the bean registrar that will be called to register
340+
* additional beans
341+
*/
342+
fun register(registrar: BeanRegistrar) {
343+
return registry.register(registrar)
344+
}
345+
337346
/**
338347
* Apply the nested block if the given profile expression matches the
339348
* active profiles.

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

+14
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ class BeanRegistrarDslConfigurationTests {
6565
assertThat(beanDefinition.resolvableType.resolveGeneric(0)).isEqualTo(Foo::class.java)
6666
}
6767

68+
@Test
69+
fun chainedBeanRegistrar() {
70+
val context = AnnotationConfigApplicationContext(ChainedBeanRegistrarKotlinConfiguration::class.java)
71+
assertThat(context.getBean<Bar>().foo).isEqualTo(context.getBean<Foo>())
72+
}
73+
6874
class Foo
6975
data class Bar(val foo: Foo)
7076
data class Baz(val message: String = "")
@@ -109,4 +115,12 @@ class BeanRegistrarDslConfigurationTests {
109115
}
110116
}
111117
})
118+
119+
@Configuration
120+
@Import(ChainedBeanRegistrar::class)
121+
internal class ChainedBeanRegistrarKotlinConfiguration
122+
123+
private class ChainedBeanRegistrar : BeanRegistrarDsl({
124+
register(SampleBeanRegistrar())
125+
})
112126
}

0 commit comments

Comments
 (0)