Skip to content

Commit 282d961

Browse files
jhoellerunknown
authored andcommitted
@import'ed configuration classes get properly registered in case of same class name
Issue: SPR-9243
1 parent 8f12d98 commit 282d961

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

spring-context/src/main/java/org/springframework/context/annotation/AnnotationBeanNameGenerator.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.Map;
2121
import java.util.Set;
2222

23+
import org.springframework.beans.factory.BeanFactoryUtils;
2324
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
2425
import org.springframework.beans.factory.config.BeanDefinition;
2526
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
@@ -73,7 +74,7 @@ public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry
7374
}
7475
}
7576
// Fallback: generate a unique default bean name.
76-
return buildDefaultBeanName(definition);
77+
return buildDefaultBeanName(definition, registry);
7778
}
7879

7980
/**
@@ -119,6 +120,26 @@ protected boolean isStereotypeWithNameValue(String annotationType,
119120
return (isStereotype && attributes != null && attributes.containsKey("value"));
120121
}
121122

123+
/**
124+
* Derive a default bean name from the given bean definition.
125+
* <p>The default implementation delegates to {@link #buildDefaultBeanName(BeanDefinition)},
126+
* appending a counter suffix if necessary to make the bean name unique in the given registry.
127+
* @param definition the bean definition to build a bean name for
128+
* @param registry the registry that the given bean definition is being registered with
129+
* @return the default bean name (never <code>null</code>)
130+
*/
131+
protected String buildDefaultBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) {
132+
String generatedName = buildDefaultBeanName(definition);
133+
// Increase counter until the id is unique.
134+
String id = generatedName;
135+
int counter = 0;
136+
while (registry.containsBeanDefinition(id)) {
137+
counter++;
138+
id = generatedName + BeanFactoryUtils.GENERATED_BEAN_NAME_SEPARATOR + counter;
139+
}
140+
return id;
141+
}
142+
122143
/**
123144
* Derive a default bean name from the given bean definition.
124145
* <p>The default implementation simply builds a decapitalized version

0 commit comments

Comments
 (0)