Add BeanRegistrarDsl.register

See gh-34557
This commit is contained in:
Sébastien Deleuze
2025-03-12 12:34:04 +01:00
parent 641b809d4b
commit 762831e742
2 changed files with 23 additions and 0 deletions

View File

@@ -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.

View File

@@ -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<Bar>().foo).isEqualTo(context.getBean<Foo>())
}
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())
})
}