mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 04:26:12 +08:00
Make @ImportAutoConfiguration not register package
Update `@ImportAutoConfiguration` so that it is no longer annotated with `@AutoConfigurationPackage` and as such isn't a marker for `AutoConfigurationPackages`. Having `@ImportAutoConfiguration` marked as an auto-configuration package is particularly problematic in tests since it frequently breaks context caching. Fixes gh-9282
This commit is contained in:
parent
2775beb2b7
commit
b9cfe21193
@ -46,7 +46,6 @@ import org.springframework.core.annotation.AliasFor;
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
@Inherited
|
||||
@AutoConfigurationPackage
|
||||
@Import(ImportAutoConfigurationImportSelector.class)
|
||||
public @interface ImportAutoConfiguration {
|
||||
|
||||
|
@ -37,6 +37,7 @@ import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.io.DefaultResourceLoader;
|
||||
import org.springframework.core.type.AnnotationMetadata;
|
||||
import org.springframework.core.type.classreading.SimpleMetadataReaderFactory;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.verifyZeroInteractions;
|
||||
@ -125,7 +126,6 @@ public class ImportAutoConfigurationImportSelectorTests {
|
||||
public void exclusionsAliasesAreApplied() throws Exception {
|
||||
AnnotationMetadata annotationMetadata = getAnnotationMetadata(
|
||||
ImportWithSelfAnnotatingAnnotationExclude.class);
|
||||
|
||||
String[] imports = this.importSelector.selectImports(annotationMetadata);
|
||||
assertThat(imports).isEmpty();
|
||||
}
|
||||
@ -182,6 +182,19 @@ public class ImportAutoConfigurationImportSelectorTests {
|
||||
assertThat(set1).isNotEqualTo(set2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void determineImportsShouldNotSetPackageImport() throws Exception {
|
||||
Class<?> packageImportClass = ClassUtils.resolveClassName(
|
||||
"org.springframework.boot.autoconfigure.AutoConfigurationPackages.PackageImport",
|
||||
null);
|
||||
Set<Object> selectedImports = this.importSelector
|
||||
.determineImports(getAnnotationMetadata(
|
||||
ImportMetaAutoConfigurationExcludeWithUnrelatedOne.class));
|
||||
for (Object selectedImport : selectedImports) {
|
||||
assertThat(selectedImport).isNotInstanceOf(packageImportClass);
|
||||
}
|
||||
}
|
||||
|
||||
private AnnotationMetadata getAnnotationMetadata(Class<?> source) throws IOException {
|
||||
return new SimpleMetadataReaderFactory().getMetadataReader(source.getName())
|
||||
.getAnnotationMetadata();
|
||||
|
@ -24,6 +24,7 @@ import org.junit.runner.notification.RunNotifier;
|
||||
import org.junit.runners.model.InitializationError;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurationPackage;
|
||||
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.domain.EntityScan;
|
||||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
|
||||
@ -94,8 +95,8 @@ public class ImportsContextCustomizerFactoryWithAutoConfigurationTests {
|
||||
|
||||
}
|
||||
|
||||
@ContextConfiguration(classes = EmptyConfig.class)
|
||||
@DataJpaTest
|
||||
@ContextConfiguration(classes = EmptyConfig.class)
|
||||
@Unrelated2
|
||||
public static class DataJpaTest2 {
|
||||
|
||||
@ -109,8 +110,8 @@ public class ImportsContextCustomizerFactoryWithAutoConfigurationTests {
|
||||
|
||||
}
|
||||
|
||||
@ContextConfiguration(classes = EmptyConfig.class)
|
||||
@DataJpaTest
|
||||
@ContextConfiguration(classes = EmptyConfig.class)
|
||||
@Unrelated1
|
||||
public static class DataJpaTest3 {
|
||||
|
||||
@ -124,8 +125,8 @@ public class ImportsContextCustomizerFactoryWithAutoConfigurationTests {
|
||||
|
||||
}
|
||||
|
||||
@ContextConfiguration(classes = EmptyConfig.class)
|
||||
@DataJpaTest(showSql = false)
|
||||
@ContextConfiguration(classes = EmptyConfig.class)
|
||||
@Unrelated1
|
||||
public static class DataJpaTest4 {
|
||||
|
||||
@ -151,6 +152,7 @@ public class ImportsContextCustomizerFactoryWithAutoConfigurationTests {
|
||||
|
||||
@Configuration
|
||||
@EntityScan(basePackageClasses = ExampleEntity.class)
|
||||
@AutoConfigurationPackage
|
||||
static class EmptyConfig {
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user