From 71f6da673abec4c805eb8232a2c8e07837b7f0a7 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Thu, 2 May 2013 09:41:20 -0700 Subject: [PATCH] Consider @Import classes as lite @Configuration Allow classes that are annotated with @Import to be considered as 'lite' @Configuration candidates. Allows the AnnotationConfigApplicationContext to directly register @Import beans even if they are not @Components. Issue: SPR-10533 --- .../annotation/ConfigurationClassUtils.java | 9 ++++++--- .../ImportAnnotationDetectionTests.java | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassUtils.java b/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassUtils.java index 645203933a..d7bed91502 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassUtils.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassUtils.java @@ -101,9 +101,12 @@ abstract class ConfigurationClassUtils { } public static boolean isLiteConfigurationCandidate(AnnotationMetadata metadata) { - return !metadata.isInterface() && // not an interface or an annotation - (metadata.isAnnotated(Component.class.getName()) || - metadata.hasAnnotatedMethods(Bean.class.getName())); + if(metadata.isInterface()) { + return false; // do not consider an interface or an annotation + } + return metadata.isAnnotated(Import.class.getName()) || + metadata.isAnnotated(Component.class.getName()) || + metadata.hasAnnotatedMethods(Bean.class.getName()); } diff --git a/spring-context/src/test/java/org/springframework/context/annotation/configuration/ImportAnnotationDetectionTests.java b/spring-context/src/test/java/org/springframework/context/annotation/configuration/ImportAnnotationDetectionTests.java index 7e5570ee4e..b24041cf92 100644 --- a/spring-context/src/test/java/org/springframework/context/annotation/configuration/ImportAnnotationDetectionTests.java +++ b/spring-context/src/test/java/org/springframework/context/annotation/configuration/ImportAnnotationDetectionTests.java @@ -25,12 +25,15 @@ import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import org.junit.Test; +import org.springframework.stereotype.Service; import org.springframework.tests.sample.beans.TestBean; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; +import static org.junit.Assert.*; + /** * Tests that @Import may be used both as a locally declared and meta-declared * annotation, that all declarations are processed, and that any local declaration @@ -70,6 +73,16 @@ public class ImportAnnotationDetectionTests { assertThat(ctx.getBean("testBean2", TestBean.class).getName(), is("2a")); } + @Test + public void importFromBean() throws Exception { + AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); + ctx.register(ImportFromBean.class); + ctx.refresh(); + assertThat(ctx.containsBean("importAnnotationDetectionTests.ImportFromBean"), is(true)); + assertThat(ctx.containsBean("testBean1"), is(true)); + assertThat(ctx.getBean("testBean1", TestBean.class).getName(), is("1")); + } + @Configuration @MetaImport1 @MetaImport2 @@ -136,4 +149,9 @@ public class ImportAnnotationDetectionTests { return new TestBean("3"); } } + + @MetaImport1 + static class ImportFromBean { + + } }