Polish support for @Import on interfaces
- Update @Import Javadoc - Move tests from ImportSelectorTests to ImportTests See gh-34820
This commit is contained in:
@@ -550,12 +550,13 @@ class ConfigurationClassParser {
|
||||
* <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>As of Spring Framework 7.0, {@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.
|
||||
* <p>As of Spring Framework 7.0, {@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
|
||||
* @param visited used to track visited classes and interfaces to prevent infinite
|
||||
* recursion
|
||||
* @throws IOException if there is any problem reading metadata from the named class
|
||||
*/
|
||||
private void collectImports(SourceClass sourceClass, Set<SourceClass> imports, Set<SourceClass> visited)
|
||||
|
||||
@@ -47,6 +47,12 @@ import org.springframework.beans.factory.BeanRegistrar;
|
||||
* directly declared imports to override beans registered via {@code @Import}
|
||||
* meta-annotations.
|
||||
*
|
||||
* <p>As of Spring Framework 7.0, {@code @Import} annotations declared on interfaces
|
||||
* implemented by {@code @Configuration} classes are also supported. Locally declared
|
||||
* {@code @Import} annotations are processed after {@code @Import} annotations on
|
||||
* interfaces, which allows local imports to override beans registered via
|
||||
* {@code @Import} annotations inherited from interfaces.
|
||||
*
|
||||
* <p>If XML or other non-{@code @Configuration} bean definition resources need to be
|
||||
* imported, use the {@link ImportResource @ImportResource} annotation instead.
|
||||
*
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2025 the original author or authors.
|
||||
* Copyright 2002-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -62,7 +62,6 @@ import static org.mockito.Mockito.spy;
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Stephane Nicoll
|
||||
* @author Daeho Kwon
|
||||
*/
|
||||
@SuppressWarnings("resource")
|
||||
public class ImportSelectorTests {
|
||||
@@ -204,71 +203,6 @@ public class ImportSelectorTests {
|
||||
assertThat(TestImportGroup.environment).isEqualTo(context.getEnvironment());
|
||||
}
|
||||
|
||||
@Test
|
||||
void importAnnotationOnImplementedInterfaceIsRespected() {
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
|
||||
InterfaceBasedConfig.class);
|
||||
|
||||
assertThat(context.getBean(ImportedConfig.class)).isNotNull();
|
||||
assertThat(context.getBean(ImportedBean.class)).isNotNull();
|
||||
assertThat(context.getBean(ImportedBean.class).name()).isEqualTo("imported");
|
||||
}
|
||||
|
||||
@Test
|
||||
void localImportShouldOverrideInterfaceImport() {
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
|
||||
OverridingConfig.class);
|
||||
|
||||
assertThat(context.getBean(ImportedConfig.class)).isNotNull();
|
||||
assertThat(context.getBean(ImportedBean.class)).isNotNull();
|
||||
assertThat(context.getBean(ImportedBean.class).name()).isEqualTo("from class");
|
||||
}
|
||||
|
||||
@Import(ImportedConfig.class)
|
||||
interface ConfigImportMarker {
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class InterfaceBasedConfig implements ConfigImportMarker {
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@Import(OverridingImportedConfig.class)
|
||||
static class OverridingConfig implements ConfigImportMarker {
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class OverridingImportedConfig {
|
||||
@Bean
|
||||
ImportedBean importedBean() {
|
||||
return new ImportedBean("from class");
|
||||
}
|
||||
}
|
||||
|
||||
static class ImportedBean {
|
||||
|
||||
private final String name;
|
||||
|
||||
ImportedBean() {
|
||||
this.name = "imported";
|
||||
}
|
||||
|
||||
ImportedBean(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
String name() {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class ImportedConfig {
|
||||
@Bean
|
||||
ImportedBean importedBean() {
|
||||
return new ImportedBean();
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@Import(SampleImportSelector.class)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2024 the original author or authors.
|
||||
* Copyright 2002-2025 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -38,6 +38,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
*
|
||||
* @author Chris Beams
|
||||
* @author Juergen Hoeller
|
||||
* @author Daeho Kwon
|
||||
*/
|
||||
class ImportTests {
|
||||
|
||||
@@ -391,4 +392,60 @@ class ImportTests {
|
||||
assertThat(ctx.getBeansOfType(SiblingImportingConfigB.class)).hasSize(1);
|
||||
}
|
||||
|
||||
@Test // gh-34820
|
||||
void importAnnotationOnImplementedInterfaceIsRespected() {
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(InterfaceBasedConfig.class);
|
||||
|
||||
assertThat(context.getBean(ImportedConfig.class)).isNotNull();
|
||||
assertThat(context.getBean(ImportedBean.class)).hasFieldOrPropertyWithValue("name", "imported");
|
||||
|
||||
context.close();
|
||||
}
|
||||
|
||||
@Test // gh-34820
|
||||
void localImportShouldOverrideInterfaceImport() {
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(OverridingConfig.class);
|
||||
|
||||
assertThat(context.getBean(ImportedConfig.class)).isNotNull();
|
||||
assertThat(context.getBean(OverridingImportedConfig.class)).isNotNull();
|
||||
assertThat(context.getBean(ImportedBean.class)).hasFieldOrPropertyWithValue("name", "from class");
|
||||
|
||||
context.close();
|
||||
}
|
||||
|
||||
|
||||
record ImportedBean(String name) {
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class ImportedConfig {
|
||||
|
||||
@Bean
|
||||
ImportedBean importedBean() {
|
||||
return new ImportedBean("imported");
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class OverridingImportedConfig {
|
||||
|
||||
@Bean
|
||||
ImportedBean importedBean() {
|
||||
return new ImportedBean("from class");
|
||||
}
|
||||
}
|
||||
|
||||
@Import(ImportedConfig.class)
|
||||
interface ConfigImportMarker {
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class InterfaceBasedConfig implements ConfigImportMarker {
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@Import(OverridingImportedConfig.class)
|
||||
static class OverridingConfig implements ConfigImportMarker {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user