From b9cfe21193b1a5eb6473150f934e5691f5bb5b58 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Mon, 24 Jul 2017 12:21:00 -0700 Subject: [PATCH] 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 --- .../autoconfigure/ImportAutoConfiguration.java | 1 - ...mportAutoConfigurationImportSelectorTests.java | 15 ++++++++++++++- ...stomizerFactoryWithAutoConfigurationTests.java | 8 +++++--- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ImportAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ImportAutoConfiguration.java index af55f05b8d..7d72a2fd2d 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ImportAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ImportAutoConfiguration.java @@ -46,7 +46,6 @@ import org.springframework.core.annotation.AliasFor; @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited -@AutoConfigurationPackage @Import(ImportAutoConfigurationImportSelector.class) public @interface ImportAutoConfiguration { diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/ImportAutoConfigurationImportSelectorTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/ImportAutoConfigurationImportSelectorTests.java index 3dfc9183b4..3c1d17980c 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/ImportAutoConfigurationImportSelectorTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/ImportAutoConfigurationImportSelectorTests.java @@ -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 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(); diff --git a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/cache/ImportsContextCustomizerFactoryWithAutoConfigurationTests.java b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/cache/ImportsContextCustomizerFactoryWithAutoConfigurationTests.java index 45d5dbbfdc..7399043c90 100644 --- a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/cache/ImportsContextCustomizerFactoryWithAutoConfigurationTests.java +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/cache/ImportsContextCustomizerFactoryWithAutoConfigurationTests.java @@ -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 { }