Skip to content

Commit 68f61f3

Browse files
committed
Fix nested @component annotation instantiation bug
3.1 M2 introduced a regression that causes false positives during @configuration class candidate checks. Now performing a call to AnnotationMetadata#isInterface in addition to checks for @component and @bean annotations when determining whether a candidate is a 'lite' configuration class. Annotations are in the end interfaces, so both are filtered out at once. Issue: SPR-8761
1 parent faba594 commit 68f61f3

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,9 @@ public static boolean isFullConfigurationCandidate(AnnotationMetadata metadata)
100100
}
101101

102102
public static boolean isLiteConfigurationCandidate(AnnotationMetadata metadata) {
103-
return metadata.isAnnotated(Component.class.getName()) ||
104-
metadata.hasAnnotatedMethods(Bean.class.getName());
103+
return !metadata.isInterface() && // not an interface or an annotation
104+
(metadata.isAnnotated(Component.class.getName()) ||
105+
metadata.hasAnnotatedMethods(Bean.class.getName()));
105106
}
106107

107108

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2002-2011 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.context.annotation.spr8761;
18+
19+
import static org.hamcrest.CoreMatchers.is;
20+
import static org.junit.Assert.assertThat;
21+
22+
import java.lang.annotation.Retention;
23+
import java.lang.annotation.RetentionPolicy;
24+
25+
import org.junit.Test;
26+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
27+
import org.springframework.stereotype.Component;
28+
29+
/**
30+
* Tests cornering the regression reported in SPR-8761.
31+
*
32+
* @author Chris Beams
33+
*/
34+
public class Spr8761Tests {
35+
36+
/**
37+
* Prior to the fix for SPR-8761, this test threw because the nested MyComponent
38+
* annotation was being falsely considered as a 'lite' Configuration class candidate.
39+
*/
40+
@Test
41+
public void repro() {
42+
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
43+
ctx.scan(getClass().getPackage().getName());
44+
ctx.refresh();
45+
assertThat(ctx.containsBean("withNestedAnnotation"), is(true));
46+
}
47+
48+
}
49+
50+
@Component
51+
class WithNestedAnnotation {
52+
53+
@Retention(RetentionPolicy.RUNTIME)
54+
@Component
55+
public static @interface MyComponent {
56+
}
57+
}

0 commit comments

Comments
 (0)