pull/6613/head
Phillip Webb 8 years ago
parent 1b17677e0b
commit f4328b41af

@ -27,8 +27,7 @@ import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.TypeFilter; import org.springframework.core.type.filter.TypeFilter;
/** /**
* A {@link TypeFilter} implementation that matches registered auto-configuration * A {@link TypeFilter} implementation that matches registered auto-configuration classes.
* classes.
* *
* @author Stephane Nicoll * @author Stephane Nicoll
* @since 1.5.0 * @since 1.5.0
@ -37,7 +36,7 @@ public class AutoConfigurationExcludeFilter implements TypeFilter, BeanClassLoad
private ClassLoader beanClassLoader; private ClassLoader beanClassLoader;
private List<String> candidateAutoConfigurations; private volatile List<String> autoConfigurations;
@Override @Override
public void setBeanClassLoader(ClassLoader beanClassLoader) { public void setBeanClassLoader(ClassLoader beanClassLoader) {
@ -50,22 +49,22 @@ public class AutoConfigurationExcludeFilter implements TypeFilter, BeanClassLoad
return isConfiguration(metadataReader) && isAutoConfiguration(metadataReader); return isConfiguration(metadataReader) && isAutoConfiguration(metadataReader);
} }
protected List<String> getCandidateAutoConfigurations() {
if (this.candidateAutoConfigurations == null) {
this.candidateAutoConfigurations = SpringFactoriesLoader.loadFactoryNames(
EnableAutoConfiguration.class, this.beanClassLoader);
}
return this.candidateAutoConfigurations;
}
private boolean isConfiguration(MetadataReader metadataReader) { private boolean isConfiguration(MetadataReader metadataReader) {
return metadataReader.getAnnotationMetadata() return metadataReader.getAnnotationMetadata()
.isAnnotated(Configuration.class.getName()); .isAnnotated(Configuration.class.getName());
} }
private boolean isAutoConfiguration(MetadataReader metadataReader) { private boolean isAutoConfiguration(MetadataReader metadataReader) {
return getCandidateAutoConfigurations().contains( return getAutoConfigurations()
metadataReader.getClassMetadata().getClassName()); .contains(metadataReader.getClassMetadata().getClassName());
}
protected List<String> getAutoConfigurations() {
if (this.autoConfigurations == null) {
this.autoConfigurations = SpringFactoriesLoader.loadFactoryNames(
EnableAutoConfiguration.class, this.beanClassLoader);
}
return this.autoConfigurations;
} }
} }

@ -41,6 +41,8 @@ import static org.assertj.core.api.Assertions.assertThat;
*/ */
public class AutoConfigurationExcludeFilterTests { public class AutoConfigurationExcludeFilterTests {
private static final Class<?> FILTERED = ExampleFilteredAutoConfiguration.class;
@Rule @Rule
public ExpectedException thrown = ExpectedException.none(); public ExpectedException thrown = ExpectedException.none();
@ -59,10 +61,9 @@ public class AutoConfigurationExcludeFilterTests {
assertThat(this.context.getBeansOfType(String.class)).hasSize(1); assertThat(this.context.getBeansOfType(String.class)).hasSize(1);
assertThat(this.context.getBean(String.class)).isEqualTo("test"); assertThat(this.context.getBean(String.class)).isEqualTo("test");
this.thrown.expect(NoSuchBeanDefinitionException.class); this.thrown.expect(NoSuchBeanDefinitionException.class);
this.context.getBean(ExampleFilteredAutoConfiguration.class); this.context.getBean(FILTERED);
} }
@Configuration @Configuration
@ComponentScan(basePackageClasses = ExampleConfiguration.class, excludeFilters = @ComponentScan.Filter(type = FilterType.CUSTOM, classes = TestAutoConfigurationExcludeFilter.class)) @ComponentScan(basePackageClasses = ExampleConfiguration.class, excludeFilters = @ComponentScan.Filter(type = FilterType.CUSTOM, classes = TestAutoConfigurationExcludeFilter.class))
static class Config { static class Config {
@ -71,10 +72,12 @@ public class AutoConfigurationExcludeFilterTests {
static class TestAutoConfigurationExcludeFilter static class TestAutoConfigurationExcludeFilter
extends AutoConfigurationExcludeFilter { extends AutoConfigurationExcludeFilter {
@Override @Override
protected List<String> getCandidateAutoConfigurations() { protected List<String> getAutoConfigurations() {
return Collections.singletonList(ExampleFilteredAutoConfiguration.class.getName()); return Collections.singletonList(FILTERED.getName());
} }
} }
} }

Loading…
Cancel
Save