Skip to content

Commit b8c1130

Browse files
committed
detect @bean methods on registered plain bean classes as well (SPR-5795)
1 parent 38110d3 commit b8c1130

File tree

4 files changed

+36
-1
lines changed

4 files changed

+36
-1
lines changed

org.springframework.context/src/main/java/org/springframework/context/annotation/ConfigurationClassPostProcessor.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,8 @@ protected boolean checkConfigurationClassCandidate(BeanDefinition beanDef) {
222222
beanDef.setAttribute(CONFIGURATION_CLASS_ATTRIBUTE, CONFIGURATION_CLASS_FULL);
223223
return true;
224224
}
225-
else if (metadata.isAnnotated(Component.class.getName())) {
225+
else if (metadata.isAnnotated(Component.class.getName()) ||
226+
metadata.hasAnnotatedMethods(Bean.class.getName())) {
226227
beanDef.setAttribute(CONFIGURATION_CLASS_ATTRIBUTE, CONFIGURATION_CLASS_LITE);
227228
return true;
228229
}

org.springframework.core/src/main/java/org/springframework/core/type/AnnotationMetadata.java

+6
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ public interface AnnotationMetadata extends ClassMetadata {
8383
*/
8484
Map<String, Object> getAnnotationAttributes(String annotationType);
8585

86+
/**
87+
* Determine whether the underlying class has any methods that are
88+
* annotated (or meta-annotated) with the given annotation type.
89+
*/
90+
boolean hasAnnotatedMethods(String annotationType);
91+
8692
/**
8793
* Retrieve the method metadata for all methods that are annotated
8894
* (or meta-annotated) with the given annotation type.

org.springframework.core/src/main/java/org/springframework/core/type/StandardAnnotationMetadata.java

+19
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,25 @@ public Map<String, Object> getAnnotationAttributes(String annotationType) {
128128
return null;
129129
}
130130

131+
public boolean hasAnnotatedMethods(String annotationType) {
132+
Method[] methods = getIntrospectedClass().getDeclaredMethods();
133+
for (Method method : methods) {
134+
for (Annotation ann : method.getAnnotations()) {
135+
if (ann.annotationType().getName().equals(annotationType)) {
136+
return true;
137+
}
138+
else {
139+
for (Annotation metaAnn : ann.annotationType().getAnnotations()) {
140+
if (metaAnn.annotationType().getName().equals(annotationType)) {
141+
return true;
142+
}
143+
}
144+
}
145+
}
146+
}
147+
return false;
148+
}
149+
131150
public Set<MethodMetadata> getAnnotatedMethods(String annotationType) {
132151
Method[] methods = getIntrospectedClass().getDeclaredMethods();
133152
Set<MethodMetadata> annotatedMethods = new LinkedHashSet<MethodMetadata>();

org.springframework.core/src/main/java/org/springframework/core/type/classreading/AnnotationMetadataReadingVisitor.java

+9
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,15 @@ public Map<String, Object> getAnnotationAttributes(String annotationType) {
100100
return this.attributeMap.get(annotationType);
101101
}
102102

103+
public boolean hasAnnotatedMethods(String annotationType) {
104+
for (MethodMetadata method : this.methodMetadataSet) {
105+
if (method.isAnnotated(annotationType)) {
106+
return true;
107+
}
108+
}
109+
return false;
110+
}
111+
103112
public Set<MethodMetadata> getAnnotatedMethods(String annotationType) {
104113
Set<MethodMetadata> annotatedMethods = new LinkedHashSet<MethodMetadata>();
105114
for (MethodMetadata method : this.methodMetadataSet) {

0 commit comments

Comments
 (0)