Skip to content

Commit 2bc213d

Browse files
committed
Allow registering aliases with BeanRegistry
Closes gh-34599
1 parent 087d239 commit 2bc213d

File tree

6 files changed

+30
-1
lines changed

6 files changed

+30
-1
lines changed

Diff for: spring-beans/src/main/java/org/springframework/beans/factory/BeanRegistry.java

+9
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@
3838
*/
3939
public interface BeanRegistry {
4040

41+
/**
42+
* Given a name, register an alias for it.
43+
* @param name the canonical name
44+
* @param alias the alias to be registered
45+
* @throws IllegalStateException if the alias is already in use
46+
* and may not be overridden
47+
*/
48+
void registerAlias(String name, String alias);
49+
4150
/**
4251
* Register a bean from the given bean class, which will be instantiated
4352
* using the related {@link BeanUtils#getResolvableConstructor resolvable constructor}

Diff for: spring-beans/src/main/java/org/springframework/beans/factory/support/BeanRegistryAdapter.java

+5
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ public BeanRegistryAdapter(BeanDefinitionRegistry beanRegistry, ListableBeanFact
8080
}
8181

8282

83+
@Override
84+
public void registerAlias(String name, String alias) {
85+
this.beanRegistry.registerAlias(name, alias);
86+
}
87+
8388
@Override
8489
public <T> String registerBean(Class<T> beanClass) {
8590
String beanName = BeanDefinitionReaderUtils.uniqueBeanName(beanClass.getName(), this.beanRegistry);

Diff for: spring-beans/src/main/kotlin/org/springframework/beans/factory/BeanRegistrarDsl.kt

+11
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,17 @@ open class BeanRegistrarDsl(private val init: BeanRegistrarDsl.() -> Unit): Bean
6464
*/
6565
lateinit var env: Environment
6666

67+
/**
68+
* Given a name, register an alias for it.
69+
* @param name the canonical name
70+
* @param alias the alias to be registered
71+
* @throws IllegalStateException if the alias is already in use
72+
* and may not be overridden
73+
*/
74+
fun registerAlias(name: String, alias: String) {
75+
registry.registerAlias(name, alias);
76+
}
77+
6778
/**
6879
* Register a bean from the given bean class, which will be instantiated
6980
* using the related [resolvable constructor]

Diff for: spring-context/src/test/java/org/springframework/context/annotation/beanregistrar/BeanRegistrarConfigurationTests.java

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public class BeanRegistrarConfigurationTests {
4545
void beanRegistrar() {
4646
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(BeanRegistrarConfiguration.class);
4747
assertThat(context.getBean(Bar.class).foo()).isEqualTo(context.getBean(Foo.class));
48+
assertThat(context.getBean("foo", Foo.class)).isEqualTo(context.getBean("fooAlias", Foo.class));
4849
assertThatThrownBy(() -> context.getBean(Baz.class)).isInstanceOf(NoSuchBeanDefinitionException.class);
4950
assertThat(context.getBean(Init.class).initialized).isTrue();
5051
BeanDefinition beanDefinition = context.getBeanDefinition("bar");

Diff for: spring-context/src/test/kotlin/org/springframework/context/annotation/BeanRegistrarDslConfigurationTests.kt

+3-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class BeanRegistrarDslConfigurationTests {
3939
fun beanRegistrar() {
4040
val context = AnnotationConfigApplicationContext(BeanRegistrarKotlinConfiguration::class.java)
4141
assertThat(context.getBean<Bar>().foo).isEqualTo(context.getBean<Foo>())
42+
assertThat(context.getBean<Foo>("foo")).isEqualTo(context.getBean<Foo>("fooAlias"))
4243
assertThatThrownBy(ThrowableAssert.ThrowingCallable { context.getBean<Baz>() }).isInstanceOf(NoSuchBeanDefinitionException::class.java)
4344
assertThat(context.getBean<Init>().initialized).isTrue()
4445
val beanDefinition = context.getBeanDefinition("bar")
@@ -88,7 +89,8 @@ class BeanRegistrarDslConfigurationTests {
8889
internal class BeanRegistrarKotlinConfiguration
8990

9091
private class SampleBeanRegistrar : BeanRegistrarDsl({
91-
registerBean<Foo>()
92+
registerBean<Foo>("foo")
93+
registerAlias("foo", "fooAlias")
9294
registerBean(
9395
name = "bar",
9496
prototype = true,

Diff for: spring-context/src/testFixtures/java/org/springframework/context/testfixture/beans/factory/SampleBeanRegistrar.java

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public class SampleBeanRegistrar implements BeanRegistrar {
2727
@Override
2828
public void register(BeanRegistry registry, Environment env) {
2929
registry.registerBean("foo", Foo.class);
30+
registry.registerAlias("foo", "fooAlias");
3031
registry.registerBean("bar", Bar.class, spec -> spec
3132
.prototype()
3233
.lazyInit()

0 commit comments

Comments
 (0)