17
17
package org .springframework .context .annotation ;
18
18
19
19
import java .beans .Introspector ;
20
+ import java .util .Collections ;
20
21
import java .util .Map ;
21
22
import java .util .Set ;
23
+ import java .util .concurrent .ConcurrentHashMap ;
22
24
23
25
import org .springframework .beans .factory .annotation .AnnotatedBeanDefinition ;
24
26
import org .springframework .beans .factory .config .BeanDefinition ;
@@ -70,6 +72,8 @@ public class AnnotationBeanNameGenerator implements BeanNameGenerator {
70
72
71
73
private static final String COMPONENT_ANNOTATION_CLASSNAME = "org.springframework.stereotype.Component" ;
72
74
75
+ private final Map <String , Set <String >> metaAnnotationTypesCache = new ConcurrentHashMap <>();
76
+
73
77
74
78
@ Override
75
79
public String generateBeanName (BeanDefinition definition , BeanDefinitionRegistry registry ) {
@@ -96,16 +100,22 @@ protected String determineBeanNameFromAnnotation(AnnotatedBeanDefinition annotat
96
100
String beanName = null ;
97
101
for (String type : types ) {
98
102
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 ;
107
118
}
108
- beanName = strVal ;
109
119
}
110
120
}
111
121
}
0 commit comments