Skip to content

Commit 824c90d

Browse files
committed
ConfigurationClassParser avoids double registration of nested classes which extend their containing class
Issue: SPR-12195
1 parent f394c8a commit 824c90d

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,8 @@ protected final SourceClass doProcessConfigurationClass(ConfigurationClass confi
311311
*/
312312
private void processMemberClasses(ConfigurationClass configClass, SourceClass sourceClass) throws IOException {
313313
for (SourceClass memberClass : sourceClass.getMemberClasses()) {
314-
if (ConfigurationClassUtils.isConfigurationCandidate(memberClass.getMetadata())) {
314+
if (ConfigurationClassUtils.isConfigurationCandidate(memberClass.getMetadata()) &&
315+
!memberClass.getMetadata().getClassName().equals(configClass.getMetadata().getClassName())) {
315316
processConfigurationClass(memberClass.asConfigClass(configClass));
316317
}
317318
}

spring-context/src/test/java/org/springframework/context/annotation/ComponentScanAnnotationIntegrationTests.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
4242
import org.springframework.context.annotation.ComponentScan.Filter;
4343
import org.springframework.context.annotation.ComponentScanParserTests.KustomAnnotationAutowiredBean;
44+
import org.springframework.context.annotation.componentscan.simple.ClassWithNestedComponents;
4445
import org.springframework.context.annotation.componentscan.simple.SimpleComponent;
4546
import org.springframework.context.support.GenericApplicationContext;
4647
import org.springframework.tests.context.SimpleMapScope;
@@ -115,6 +116,8 @@ public void viaContextRegistration_WithComposedAnnotation() {
115116
ctx.refresh();
116117
ctx.getBean(ComposedAnnotationConfig.class);
117118
ctx.getBean(SimpleComponent.class);
119+
ctx.getBean(ClassWithNestedComponents.NestedComponent.class);
120+
ctx.getBean(ClassWithNestedComponents.OtherNestedComponent.class);
118121
assertThat("config class bean not found",
119122
ctx.containsBeanDefinition("componentScanAnnotationIntegrationTests.ComposedAnnotationConfig"), is(true));
120123
assertThat("@ComponentScan annotated @Configuration class registered directly against " +
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2002-2014 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.componentscan.simple;
18+
19+
import org.springframework.stereotype.Component;
20+
21+
public class ClassWithNestedComponents {
22+
23+
@Component
24+
public static class NestedComponent extends ClassWithNestedComponents {
25+
}
26+
27+
@Component
28+
public static class OtherNestedComponent extends ClassWithNestedComponents {
29+
}
30+
31+
}

0 commit comments

Comments
 (0)