Allow registering aliases with BeanRegistry
Closes gh-34599
This commit is contained in:
@@ -38,6 +38,15 @@ import org.springframework.core.env.Environment;
|
||||
*/
|
||||
public interface BeanRegistry {
|
||||
|
||||
/**
|
||||
* Given a name, register an alias for it.
|
||||
* @param name the canonical name
|
||||
* @param alias the alias to be registered
|
||||
* @throws IllegalStateException if the alias is already in use
|
||||
* and may not be overridden
|
||||
*/
|
||||
void registerAlias(String name, String alias);
|
||||
|
||||
/**
|
||||
* Register a bean from the given bean class, which will be instantiated
|
||||
* using the related {@link BeanUtils#getResolvableConstructor resolvable constructor}
|
||||
|
||||
@@ -80,6 +80,11 @@ public class BeanRegistryAdapter implements BeanRegistry {
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void registerAlias(String name, String alias) {
|
||||
this.beanRegistry.registerAlias(name, alias);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> String registerBean(Class<T> beanClass) {
|
||||
String beanName = BeanDefinitionReaderUtils.uniqueBeanName(beanClass.getName(), this.beanRegistry);
|
||||
|
||||
@@ -64,6 +64,17 @@ open class BeanRegistrarDsl(private val init: BeanRegistrarDsl.() -> Unit): Bean
|
||||
*/
|
||||
lateinit var env: Environment
|
||||
|
||||
/**
|
||||
* Given a name, register an alias for it.
|
||||
* @param name the canonical name
|
||||
* @param alias the alias to be registered
|
||||
* @throws IllegalStateException if the alias is already in use
|
||||
* and may not be overridden
|
||||
*/
|
||||
fun registerAlias(name: String, alias: String) {
|
||||
registry.registerAlias(name, alias);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a bean from the given bean class, which will be instantiated
|
||||
* using the related [resolvable constructor]
|
||||
|
||||
@@ -45,6 +45,7 @@ public class BeanRegistrarConfigurationTests {
|
||||
void beanRegistrar() {
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(BeanRegistrarConfiguration.class);
|
||||
assertThat(context.getBean(Bar.class).foo()).isEqualTo(context.getBean(Foo.class));
|
||||
assertThat(context.getBean("foo", Foo.class)).isEqualTo(context.getBean("fooAlias", Foo.class));
|
||||
assertThatThrownBy(() -> context.getBean(Baz.class)).isInstanceOf(NoSuchBeanDefinitionException.class);
|
||||
assertThat(context.getBean(Init.class).initialized).isTrue();
|
||||
BeanDefinition beanDefinition = context.getBeanDefinition("bar");
|
||||
|
||||
@@ -39,6 +39,7 @@ class BeanRegistrarDslConfigurationTests {
|
||||
fun beanRegistrar() {
|
||||
val context = AnnotationConfigApplicationContext(BeanRegistrarKotlinConfiguration::class.java)
|
||||
assertThat(context.getBean<Bar>().foo).isEqualTo(context.getBean<Foo>())
|
||||
assertThat(context.getBean<Foo>("foo")).isEqualTo(context.getBean<Foo>("fooAlias"))
|
||||
assertThatThrownBy(ThrowableAssert.ThrowingCallable { context.getBean<Baz>() }).isInstanceOf(NoSuchBeanDefinitionException::class.java)
|
||||
assertThat(context.getBean<Init>().initialized).isTrue()
|
||||
val beanDefinition = context.getBeanDefinition("bar")
|
||||
@@ -88,7 +89,8 @@ class BeanRegistrarDslConfigurationTests {
|
||||
internal class BeanRegistrarKotlinConfiguration
|
||||
|
||||
private class SampleBeanRegistrar : BeanRegistrarDsl({
|
||||
registerBean<Foo>()
|
||||
registerBean<Foo>("foo")
|
||||
registerAlias("foo", "fooAlias")
|
||||
registerBean(
|
||||
name = "bar",
|
||||
prototype = true,
|
||||
|
||||
@@ -27,6 +27,7 @@ public class SampleBeanRegistrar implements BeanRegistrar {
|
||||
@Override
|
||||
public void register(BeanRegistry registry, Environment env) {
|
||||
registry.registerBean("foo", Foo.class);
|
||||
registry.registerAlias("foo", "fooAlias");
|
||||
registry.registerBean("bar", Bar.class, spec -> spec
|
||||
.prototype()
|
||||
.lazyInit()
|
||||
|
||||
Reference in New Issue
Block a user