Skip to content

Commit b6d32ef

Browse files
committed
Bean definition DSL generates unique bean names for bean classes
Issue: SPR-17242
1 parent b83bc39 commit b6d32ef

File tree

3 files changed

+30
-24
lines changed

3 files changed

+30
-24
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/support/BeanDefinitionReaderUtils.java

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,29 @@ else if (definition.getFactoryBeanName() != null) {
124124
id = generatedBeanName + GENERATED_BEAN_NAME_SEPARATOR + ObjectUtils.getIdentityHexString(definition);
125125
}
126126
else {
127-
// Top-level bean: use plain class name.
128-
// Increase counter until the id is unique.
129-
int counter = -1;
130-
while (counter == -1 || registry.containsBeanDefinition(id)) {
131-
counter++;
132-
id = generatedBeanName + GENERATED_BEAN_NAME_SEPARATOR + counter;
133-
}
127+
// Top-level bean: use plain class name with unique suffix if necessary.
128+
return uniqueBeanName(generatedBeanName, registry);
129+
}
130+
return id;
131+
}
132+
133+
/**
134+
* Turn the given bean name into a unique bean name for the given bean factory,
135+
* appending a unique counter as suffix if necessary.
136+
* @param beanName the original bean name
137+
* @param registry the bean factory that the definition is going to be
138+
* registered with (to check for existing bean names)
139+
* @return the unique bean name to use
140+
* @since 5.1
141+
*/
142+
public static String uniqueBeanName(String beanName, BeanDefinitionRegistry registry) {
143+
String id = beanName;
144+
int counter = -1;
145+
146+
// Increase counter until the id is unique.
147+
while (counter == -1 || registry.containsBeanDefinition(id)) {
148+
counter++;
149+
id = beanName + GENERATED_BEAN_NAME_SEPARATOR + counter;
134150
}
135151
return id;
136152
}

spring-context/src/main/kotlin/org/springframework/context/support/BeanDefinitionDsl.kt

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package org.springframework.context.support
1818

1919
import org.springframework.beans.factory.config.BeanDefinitionCustomizer
2020
import org.springframework.beans.factory.support.AbstractBeanDefinition
21+
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils
2122
import org.springframework.context.ApplicationContextInitializer
2223
import org.springframework.core.env.ConfigurableEnvironment
2324
import java.util.function.Supplier
@@ -167,11 +168,8 @@ open class BeanDefinitionDsl(private val init: BeanDefinitionDsl.() -> Unit,
167168
}
168169
}
169170

170-
when (name) {
171-
null -> context.registerBean(T::class.java, customizer)
172-
else -> context.registerBean(name, T::class.java, customizer)
173-
}
174-
171+
val beanName = name ?: BeanDefinitionReaderUtils.uniqueBeanName(T::class.java.name, context);
172+
context.registerBean(beanName, T::class.java, customizer)
175173
}
176174

177175
/**
@@ -207,13 +205,8 @@ open class BeanDefinitionDsl(private val init: BeanDefinitionDsl.() -> Unit,
207205
}
208206

209207

210-
when (name) {
211-
null -> context.registerBean(T::class.java,
212-
Supplier { function.invoke() }, customizer)
213-
else -> context.registerBean(name, T::class.java,
214-
Supplier { function.invoke() }, customizer)
215-
}
216-
208+
val beanName = name ?: BeanDefinitionReaderUtils.uniqueBeanName(T::class.java.name, context);
209+
context.registerBean(beanName, T::class.java, Supplier { function.invoke() }, customizer)
217210
}
218211

219212
/**

spring-context/src/test/kotlin/org/springframework/context/support/BeanDefinitionDslTests.kt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ class BeanDefinitionDslTests {
3434
val beans = beans {
3535
bean<Foo>()
3636
bean<Bar>("bar", scope = Scope.PROTOTYPE)
37-
bean { Baz(ref()) }
3837
bean { Baz(ref("bar")) }
3938
}
4039

@@ -59,7 +58,6 @@ class BeanDefinitionDslTests {
5958
}
6059
}
6160
profile("baz") {
62-
bean { Baz(ref()) }
6361
bean { Baz(ref("bar")) }
6462
}
6563
}
@@ -89,7 +87,6 @@ class BeanDefinitionDslTests {
8987
bean { FooFoo(env["name"]!!) }
9088
}
9189
environment( { activeProfiles.contains("baz") } ) {
92-
bean { Baz(ref()) }
9390
bean { Baz(ref("bar")) }
9491
}
9592
}
@@ -133,8 +130,8 @@ class BeanDefinitionDslTests {
133130
@Test // SPR-16269
134131
fun `Provide access to the context for allowing calling advanced features like getBeansOfType`() {
135132
val beans = beans {
136-
bean<Foo>("foo1")
137-
bean<Foo>("foo2")
133+
bean<Foo>()
134+
bean<Foo>()
138135
bean { BarBar(context.getBeansOfType<Foo>().values) }
139136
}
140137

0 commit comments

Comments
 (0)