Add support for target type to BeanRegistry
Closes gh-34560
This commit is contained in:
@@ -21,12 +21,15 @@ import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.BeanRegistrar;
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.testfixture.beans.factory.GenericBeanRegistrar;
|
||||
import org.springframework.context.testfixture.beans.factory.SampleBeanRegistrar.Bar;
|
||||
import org.springframework.context.testfixture.beans.factory.SampleBeanRegistrar.Baz;
|
||||
import org.springframework.context.testfixture.beans.factory.SampleBeanRegistrar.Foo;
|
||||
import org.springframework.context.testfixture.beans.factory.SampleBeanRegistrar.Init;
|
||||
import org.springframework.context.testfixture.context.annotation.registrar.BeanRegistrarConfiguration;
|
||||
import org.springframework.context.testfixture.context.annotation.registrar.GenericBeanRegistrarConfiguration;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
@@ -69,4 +72,13 @@ public class BeanRegistrarConfigurationTests {
|
||||
assertThat(context.getBean(Init.class).initialized).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void beanRegistrarWithTargetType() {
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
||||
context.register(GenericBeanRegistrarConfiguration.class);
|
||||
context.refresh();
|
||||
RootBeanDefinition beanDefinition = (RootBeanDefinition)context.getBeanDefinition("fooSupplier");
|
||||
assertThat(beanDefinition.getResolvableType().resolveGeneric(0)).isEqualTo(GenericBeanRegistrar.Foo.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user