Skip to content

Commit 574c740

Browse files
committed
Support @import on interfaces
Closes gh-34805 Signed-off-by: Daeho Kwon <[email protected]>
1 parent 52265a5 commit 574c740

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

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

+8
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
* @author Phillip Webb
9999
* @author Sam Brannen
100100
* @author Stephane Nicoll
101+
* @author Daeho Kwon
101102
* @since 3.0
102103
* @see ConfigurationClassBeanDefinitionReader
103104
*/
@@ -549,6 +550,9 @@ private Set<SourceClass> getImports(SourceClass sourceClass) throws IOException
549550
* <p>For example, it is common for a {@code @Configuration} class to declare direct
550551
* {@code @Import}s in addition to meta-imports originating from an {@code @Enable}
551552
* annotation.
553+
* <p>In addition, {@code @Import} annotations declared on interfaces implemented by
554+
* the configuration class are also considered. This allows imports to be triggered
555+
* indirectly via marker interfaces or shared base interfaces.
552556
* @param sourceClass the class to search
553557
* @param imports the imports collected so far
554558
* @param visited used to track visited classes to prevent infinite recursion
@@ -565,6 +569,10 @@ private void collectImports(SourceClass sourceClass, Set<SourceClass> imports, S
565569
}
566570
}
567571
imports.addAll(sourceClass.getAnnotationAttributes(Import.class.getName(), "value"));
572+
573+
for (SourceClass ifc : sourceClass.getInterfaces()) {
574+
collectImports(ifc, imports, visited);
575+
}
568576
}
569577
}
570578

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

+33
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
*
6363
* @author Phillip Webb
6464
* @author Stephane Nicoll
65+
* @author Daeho Kwon
6566
*/
6667
@SuppressWarnings("resource")
6768
public class ImportSelectorTests {
@@ -203,6 +204,38 @@ void invokeAwareMethodsInImportGroup() {
203204
assertThat(TestImportGroup.environment).isEqualTo(context.getEnvironment());
204205
}
205206

207+
@Test
208+
void importAnnotationOnImplementedInterfaceIsRespected() {
209+
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
210+
context.register(InterfaceBasedConfig.class);
211+
context.refresh();
212+
213+
assertThat(context.getBean(ImportedConfig.class)).isNotNull();
214+
assertThat(context.getBean(ImportedBean.class)).isNotNull();
215+
assertThat(context.getBean(ImportedBean.class).name()).isEqualTo("imported");
216+
}
217+
218+
@Import(ImportedConfig.class)
219+
interface ConfigImportMarker {
220+
}
221+
222+
@Configuration
223+
static class InterfaceBasedConfig implements ConfigImportMarker {
224+
}
225+
226+
static class ImportedBean {
227+
String name() {
228+
return "imported";
229+
}
230+
}
231+
232+
@Configuration
233+
static class ImportedConfig {
234+
@Bean
235+
ImportedBean importedBean() {
236+
return new ImportedBean();
237+
}
238+
}
206239

207240
@Configuration
208241
@Import(SampleImportSelector.class)

0 commit comments

Comments
 (0)