Skip to content

Commit e556307

Browse files
committed
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.
1 parent 693f6dd commit e556307

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,12 @@ public static boolean isFullConfigurationCandidate(AnnotationMetadata metadata)
101101
}
102102

103103
public static boolean isLiteConfigurationCandidate(AnnotationMetadata metadata) {
104-
return !metadata.isInterface() && // not an interface or an annotation
105-
(metadata.isAnnotated(Component.class.getName()) ||
106-
metadata.hasAnnotatedMethods(Bean.class.getName()));
104+
if(metadata.isInterface()) {
105+
return false; // do not consider an interface or an annotation
106+
}
107+
return metadata.isAnnotated(Import.class.getName()) ||
108+
metadata.isAnnotated(Component.class.getName()) ||
109+
metadata.hasAnnotatedMethods(Bean.class.getName());
107110
}
108111

109112

spring-context/src/test/java/org/springframework/context/annotation/configuration/ImportAnnotationDetectionTests.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,15 @@
2525
import java.lang.annotation.Target;
2626

2727
import org.junit.Test;
28+
import org.springframework.stereotype.Service;
2829
import org.springframework.tests.sample.beans.TestBean;
2930
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
3031
import org.springframework.context.annotation.Bean;
3132
import org.springframework.context.annotation.Configuration;
3233
import org.springframework.context.annotation.Import;
3334

35+
import static org.junit.Assert.*;
36+
3437
/**
3538
* Tests that @Import may be used both as a locally declared and meta-declared
3639
* annotation, that all declarations are processed, and that any local declaration
@@ -70,6 +73,16 @@ public void localImportIsProcessedLast() {
7073
assertThat(ctx.getBean("testBean2", TestBean.class).getName(), is("2a"));
7174
}
7275

76+
@Test
77+
public void importFromBean() throws Exception {
78+
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
79+
ctx.register(ImportFromBean.class);
80+
ctx.refresh();
81+
assertThat(ctx.containsBean("importAnnotationDetectionTests.ImportFromBean"), is(true));
82+
assertThat(ctx.containsBean("testBean1"), is(true));
83+
assertThat(ctx.getBean("testBean1", TestBean.class).getName(), is("1"));
84+
}
85+
7386
@Configuration
7487
@MetaImport1
7588
@MetaImport2
@@ -136,4 +149,9 @@ TestBean testBean3() {
136149
return new TestBean("3");
137150
}
138151
}
152+
153+
@MetaImport1
154+
static class ImportFromBean {
155+
156+
}
139157
}

0 commit comments

Comments
 (0)