Add support for target type to BeanRegistry

Closes gh-34560
This commit is contained in:
Sébastien Deleuze
2025-03-10 10:14:28 +01:00
parent c74f897fac
commit a0e2d3a221
8 changed files with 173 additions and 1 deletions

View File

@@ -20,11 +20,13 @@ import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.assertThatThrownBy
import org.assertj.core.api.ThrowableAssert
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.BeanRegistrarDsl
import org.springframework.beans.factory.InitializingBean
import org.springframework.beans.factory.NoSuchBeanDefinitionException
import org.springframework.beans.factory.config.BeanDefinition
import org.springframework.beans.factory.getBean
import org.springframework.beans.factory.BeanRegistrarDsl
import org.springframework.beans.factory.support.RootBeanDefinition
import java.util.function.Supplier
/**
* Kotlin tests leveraging [BeanRegistrarDsl].
@@ -56,6 +58,13 @@ class BeanRegistrarDslConfigurationTests {
assertThat(context.getBean<Init>().initialized).isTrue()
}
@Test
fun genericBeanRegistrar() {
val context = AnnotationConfigApplicationContext(GenericBeanRegistrarKotlinConfiguration::class.java)
val beanDefinition = context.getBeanDefinition("fooSupplier") as RootBeanDefinition
assertThat(beanDefinition.resolvableType.resolveGeneric(0)).isEqualTo(Foo::class.java)
}
class Foo
data class Bar(val foo: Foo)
data class Baz(val message: String = "")
@@ -86,4 +95,18 @@ class BeanRegistrarDslConfigurationTests {
}
registerBean<Init>()
})
@Configuration
@Import(GenericBeanRegistrar::class)
internal class GenericBeanRegistrarKotlinConfiguration
private class GenericBeanRegistrar : BeanRegistrarDsl({
registerBean<Supplier<Foo>>(name = "fooSupplier") {
object: Supplier<Foo> {
override fun get(): Foo {
return Foo()
}
}
}
})
}