|
20 | 20 | import java.util.Map;
|
21 | 21 | import java.util.Set;
|
22 | 22 |
|
| 23 | +import org.springframework.beans.factory.BeanFactoryUtils; |
23 | 24 | import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
|
24 | 25 | import org.springframework.beans.factory.config.BeanDefinition;
|
25 | 26 | import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
@@ -73,7 +74,7 @@ public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry
|
73 | 74 | }
|
74 | 75 | }
|
75 | 76 | // Fallback: generate a unique default bean name.
|
76 |
| - return buildDefaultBeanName(definition); |
| 77 | + return buildDefaultBeanName(definition, registry); |
77 | 78 | }
|
78 | 79 |
|
79 | 80 | /**
|
@@ -119,6 +120,26 @@ protected boolean isStereotypeWithNameValue(String annotationType,
|
119 | 120 | return (isStereotype && attributes != null && attributes.containsKey("value"));
|
120 | 121 | }
|
121 | 122 |
|
| 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 | + |
122 | 143 | /**
|
123 | 144 | * Derive a default bean name from the given bean definition.
|
124 | 145 | * <p>The default implementation simply builds a decapitalized version
|
|
0 commit comments