diff --git a/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java b/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java index 452823b4af..8458342e68 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java @@ -311,7 +311,8 @@ class ConfigurationClassParser { */ private void processMemberClasses(ConfigurationClass configClass, SourceClass sourceClass) throws IOException { for (SourceClass memberClass : sourceClass.getMemberClasses()) { - if (ConfigurationClassUtils.isConfigurationCandidate(memberClass.getMetadata())) { + if (ConfigurationClassUtils.isConfigurationCandidate(memberClass.getMetadata()) && + !memberClass.getMetadata().getClassName().equals(configClass.getMetadata().getClassName())) { processConfigurationClass(memberClass.asConfigClass(configClass)); } } diff --git a/spring-context/src/test/java/org/springframework/context/annotation/ComponentScanAnnotationIntegrationTests.java b/spring-context/src/test/java/org/springframework/context/annotation/ComponentScanAnnotationIntegrationTests.java index 877e445db0..a3986cd6fe 100644 --- a/spring-context/src/test/java/org/springframework/context/annotation/ComponentScanAnnotationIntegrationTests.java +++ b/spring-context/src/test/java/org/springframework/context/annotation/ComponentScanAnnotationIntegrationTests.java @@ -41,6 +41,7 @@ import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.context.annotation.ComponentScan.Filter; import org.springframework.context.annotation.ComponentScanParserTests.KustomAnnotationAutowiredBean; +import org.springframework.context.annotation.componentscan.simple.ClassWithNestedComponents; import org.springframework.context.annotation.componentscan.simple.SimpleComponent; import org.springframework.context.support.GenericApplicationContext; import org.springframework.tests.context.SimpleMapScope; @@ -115,6 +116,8 @@ public class ComponentScanAnnotationIntegrationTests { ctx.refresh(); ctx.getBean(ComposedAnnotationConfig.class); ctx.getBean(SimpleComponent.class); + ctx.getBean(ClassWithNestedComponents.NestedComponent.class); + ctx.getBean(ClassWithNestedComponents.OtherNestedComponent.class); assertThat("config class bean not found", ctx.containsBeanDefinition("componentScanAnnotationIntegrationTests.ComposedAnnotationConfig"), is(true)); assertThat("@ComponentScan annotated @Configuration class registered directly against " + diff --git a/spring-context/src/test/java/org/springframework/context/annotation/componentscan/simple/ClassWithNestedComponents.java b/spring-context/src/test/java/org/springframework/context/annotation/componentscan/simple/ClassWithNestedComponents.java new file mode 100644 index 0000000000..9a56f621d2 --- /dev/null +++ b/spring-context/src/test/java/org/springframework/context/annotation/componentscan/simple/ClassWithNestedComponents.java @@ -0,0 +1,31 @@ +/* + * Copyright 2002-2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.context.annotation.componentscan.simple; + +import org.springframework.stereotype.Component; + +public class ClassWithNestedComponents { + + @Component + public static class NestedComponent extends ClassWithNestedComponents { + } + + @Component + public static class OtherNestedComponent extends ClassWithNestedComponents { + } + +}