Skip to content

Commit 57f1e86

Browse files
committed
AnnotationBeanNameGenerator caches meta-annotations for stereotype check
Closes gh-24980
1 parent 73fadd8 commit 57f1e86

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

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

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
package org.springframework.context.annotation;
1818

1919
import java.beans.Introspector;
20+
import java.util.Collections;
2021
import java.util.Map;
2122
import java.util.Set;
23+
import java.util.concurrent.ConcurrentHashMap;
2224

2325
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
2426
import org.springframework.beans.factory.config.BeanDefinition;
@@ -70,6 +72,8 @@ public class AnnotationBeanNameGenerator implements BeanNameGenerator {
7072

7173
private static final String COMPONENT_ANNOTATION_CLASSNAME = "org.springframework.stereotype.Component";
7274

75+
private final Map<String, Set<String>> metaAnnotationTypesCache = new ConcurrentHashMap<>();
76+
7377

7478
@Override
7579
public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) {
@@ -96,16 +100,22 @@ protected String determineBeanNameFromAnnotation(AnnotatedBeanDefinition annotat
96100
String beanName = null;
97101
for (String type : types) {
98102
AnnotationAttributes attributes = AnnotationConfigUtils.attributesFor(amd, type);
99-
if (attributes != null && isStereotypeWithNameValue(type, amd.getMetaAnnotationTypes(type), attributes)) {
100-
Object value = attributes.get("value");
101-
if (value instanceof String) {
102-
String strVal = (String) value;
103-
if (StringUtils.hasLength(strVal)) {
104-
if (beanName != null && !strVal.equals(beanName)) {
105-
throw new IllegalStateException("Stereotype annotations suggest inconsistent " +
106-
"component names: '" + beanName + "' versus '" + strVal + "'");
103+
if (attributes != null) {
104+
Set<String> metaTypes = this.metaAnnotationTypesCache.computeIfAbsent(type, key -> {
105+
Set<String> result = amd.getMetaAnnotationTypes(key);
106+
return (result.isEmpty() ? Collections.emptySet() : result);
107+
});
108+
if (isStereotypeWithNameValue(type, metaTypes, attributes)) {
109+
Object value = attributes.get("value");
110+
if (value instanceof String) {
111+
String strVal = (String) value;
112+
if (StringUtils.hasLength(strVal)) {
113+
if (beanName != null && !strVal.equals(beanName)) {
114+
throw new IllegalStateException("Stereotype annotations suggest inconsistent " +
115+
"component names: '" + beanName + "' versus '" + strVal + "'");
116+
}
117+
beanName = strVal;
107118
}
108-
beanName = strVal;
109119
}
110120
}
111121
}

0 commit comments

Comments
 (0)