Skip to content

Support @Import on interfaces #34820

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
* @author Phillip Webb
* @author Sam Brannen
* @author Stephane Nicoll
* @author Daeho Kwon
* @since 3.0
* @see ConfigurationClassBeanDefinitionReader
*/
Expand Down Expand Up @@ -549,6 +550,9 @@ private Set<SourceClass> getImports(SourceClass sourceClass) throws IOException
* <p>For example, it is common for a {@code @Configuration} class to declare direct
* {@code @Import}s in addition to meta-imports originating from an {@code @Enable}
* annotation.
* <p>In addition, {@code @Import} annotations declared on interfaces implemented by
* the configuration class are also considered. This allows imports to be triggered
* indirectly via marker interfaces or shared base interfaces.
* @param sourceClass the class to search
* @param imports the imports collected so far
* @param visited used to track visited classes to prevent infinite recursion
Expand All @@ -565,6 +569,10 @@ private void collectImports(SourceClass sourceClass, Set<SourceClass> imports, S
}
}
imports.addAll(sourceClass.getAnnotationAttributes(Import.class.getName(), "value"));

for (SourceClass ifc : sourceClass.getInterfaces()) {
collectImports(ifc, imports, visited);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
*
* @author Phillip Webb
* @author Stephane Nicoll
* @author Daeho Kwon
*/
@SuppressWarnings("resource")
public class ImportSelectorTests {
Expand Down Expand Up @@ -203,6 +204,38 @@ void invokeAwareMethodsInImportGroup() {
assertThat(TestImportGroup.environment).isEqualTo(context.getEnvironment());
}

@Test
void importAnnotationOnImplementedInterfaceIsRespected() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(InterfaceBasedConfig.class);
context.refresh();

assertThat(context.getBean(ImportedConfig.class)).isNotNull();
assertThat(context.getBean(ImportedBean.class)).isNotNull();
assertThat(context.getBean(ImportedBean.class).name()).isEqualTo("imported");
}

@Import(ImportedConfig.class)
interface ConfigImportMarker {
}

@Configuration
static class InterfaceBasedConfig implements ConfigImportMarker {
}

static class ImportedBean {
String name() {
return "imported";
}
}

@Configuration
static class ImportedConfig {
@Bean
ImportedBean importedBean() {
return new ImportedBean();
}
}

@Configuration
@Import(SampleImportSelector.class)
Expand Down
Loading