Bean definition DSL generates unique bean names for bean classes
Issue: SPR-17242
This commit is contained in:
@@ -124,13 +124,29 @@ public abstract class BeanDefinitionReaderUtils {
|
||||
id = generatedBeanName + GENERATED_BEAN_NAME_SEPARATOR + ObjectUtils.getIdentityHexString(definition);
|
||||
}
|
||||
else {
|
||||
// Top-level bean: use plain class name.
|
||||
// Increase counter until the id is unique.
|
||||
int counter = -1;
|
||||
while (counter == -1 || registry.containsBeanDefinition(id)) {
|
||||
counter++;
|
||||
id = generatedBeanName + GENERATED_BEAN_NAME_SEPARATOR + counter;
|
||||
}
|
||||
// Top-level bean: use plain class name with unique suffix if necessary.
|
||||
return uniqueBeanName(generatedBeanName, registry);
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Turn the given bean name into a unique bean name for the given bean factory,
|
||||
* appending a unique counter as suffix if necessary.
|
||||
* @param beanName the original bean name
|
||||
* @param registry the bean factory that the definition is going to be
|
||||
* registered with (to check for existing bean names)
|
||||
* @return the unique bean name to use
|
||||
* @since 5.1
|
||||
*/
|
||||
public static String uniqueBeanName(String beanName, BeanDefinitionRegistry registry) {
|
||||
String id = beanName;
|
||||
int counter = -1;
|
||||
|
||||
// Increase counter until the id is unique.
|
||||
while (counter == -1 || registry.containsBeanDefinition(id)) {
|
||||
counter++;
|
||||
id = beanName + GENERATED_BEAN_NAME_SEPARATOR + counter;
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user