Skip to content

Commit a4d5800

Browse files
kwondh5217sbrannen
authored andcommitted
Support @⁠Import on interfaces
See gh-34805 Closes gh-34820 Signed-off-by: Daeho Kwon <[email protected]>
1 parent 6867051 commit a4d5800

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

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

Lines changed: 7 additions & 0 deletions
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>As of Spring Framework 7.0, {@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
@@ -558,6 +562,9 @@ private void collectImports(SourceClass sourceClass, Set<SourceClass> imports, S
558562
throws IOException {
559563

560564
if (visited.add(sourceClass)) {
565+
for (SourceClass ifc : sourceClass.getInterfaces()) {
566+
collectImports(ifc, imports, visited);
567+
}
561568
for (SourceClass annotation : sourceClass.getAnnotations()) {
562569
String annName = annotation.getMetadata().getClassName();
563570
if (!annName.equals(Import.class.getName())) {

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

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -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,71 @@ void invokeAwareMethodsInImportGroup() {
203204
assertThat(TestImportGroup.environment).isEqualTo(context.getEnvironment());
204205
}
205206

207+
@Test
208+
void importAnnotationOnImplementedInterfaceIsRespected() {
209+
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
210+
InterfaceBasedConfig.class);
211+
212+
assertThat(context.getBean(ImportedConfig.class)).isNotNull();
213+
assertThat(context.getBean(ImportedBean.class)).isNotNull();
214+
assertThat(context.getBean(ImportedBean.class).name()).isEqualTo("imported");
215+
}
216+
217+
@Test
218+
void localImportShouldOverrideInterfaceImport() {
219+
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
220+
OverridingConfig.class);
221+
222+
assertThat(context.getBean(ImportedConfig.class)).isNotNull();
223+
assertThat(context.getBean(ImportedBean.class)).isNotNull();
224+
assertThat(context.getBean(ImportedBean.class).name()).isEqualTo("from class");
225+
}
226+
227+
@Import(ImportedConfig.class)
228+
interface ConfigImportMarker {
229+
}
230+
231+
@Configuration
232+
static class InterfaceBasedConfig implements ConfigImportMarker {
233+
}
234+
235+
@Configuration
236+
@Import(OverridingImportedConfig.class)
237+
static class OverridingConfig implements ConfigImportMarker {
238+
}
239+
240+
@Configuration
241+
static class OverridingImportedConfig {
242+
@Bean
243+
ImportedBean importedBean() {
244+
return new ImportedBean("from class");
245+
}
246+
}
247+
248+
static class ImportedBean {
249+
250+
private final String name;
251+
252+
ImportedBean() {
253+
this.name = "imported";
254+
}
255+
256+
ImportedBean(String name) {
257+
this.name = name;
258+
}
259+
260+
String name() {
261+
return name;
262+
}
263+
}
264+
265+
@Configuration
266+
static class ImportedConfig {
267+
@Bean
268+
ImportedBean importedBean() {
269+
return new ImportedBean();
270+
}
271+
}
206272

207273
@Configuration
208274
@Import(SampleImportSelector.class)

0 commit comments

Comments
 (0)