From 824c90d2bf698ac2f8bee0e3f2792c5162000490 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 16 Sep 2014 17:08:30 +0200 Subject: [PATCH] ConfigurationClassParser avoids double registration of nested classes which extend their containing class Issue: SPR-12195 --- .../annotation/ConfigurationClassParser.java | 3 +- ...mponentScanAnnotationIntegrationTests.java | 3 ++ .../simple/ClassWithNestedComponents.java | 31 +++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 spring-context/src/test/java/org/springframework/context/annotation/componentscan/simple/ClassWithNestedComponents.java 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 452823b4af0..8458342e688 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 877e445db05..a3986cd6fe0 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 00000000000..9a56f621d2a --- /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 { + } + +}