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
This commit is contained in:
Sébastien Deleuze
2025-05-19 12:56:05 +02:00
parent 81ea754ac2
commit aa9ab8e545
2 changed files with 693 additions and 0 deletions

View File

@@ -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<Boo>()).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<Init>()
registerBean(::booFactory, "fooFactory")
})
@Configuration
@@ -126,3 +129,5 @@ class BeanRegistrarDslConfigurationTests {
register(SampleBeanRegistrar())
})
}
fun booFactory() = BeanRegistrarDslConfigurationTests.Boo("booFactory")