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;
/**
* A {@link TypeFilter} implementation that matches registered auto-configuration
* classes.
* A {@link TypeFilter} implementation that matches registered auto-configuration classes.
*
* @author Stephane Nicoll
* @since 1.5.0
@ -37,7 +36,7 @@ public class AutoConfigurationExcludeFilter implements TypeFilter, BeanClassLoad
private ClassLoader beanClassLoader;
private List<String> candidateAutoConfigurations;
private volatile List<String> autoConfigurations;
@Override
public void setBeanClassLoader(ClassLoader beanClassLoader) {
@ -50,22 +49,22 @@ public class AutoConfigurationExcludeFilter implements TypeFilter, BeanClassLoad
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) {
return metadataReader.getAnnotationMetadata()
.isAnnotated(Configuration.class.getName());
}
private boolean isAutoConfiguration(MetadataReader metadataReader) {
return getCandidateAutoConfigurations().contains(
metadataReader.getClassMetadata().getClassName());
return getAutoConfigurations()
.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 {
private static final Class<?> FILTERED = ExampleFilteredAutoConfiguration.class;
@Rule
public ExpectedException thrown = ExpectedException.none();
@ -59,10 +61,9 @@ public class AutoConfigurationExcludeFilterTests {
assertThat(this.context.getBeansOfType(String.class)).hasSize(1);
assertThat(this.context.getBean(String.class)).isEqualTo("test");
this.thrown.expect(NoSuchBeanDefinitionException.class);
this.context.getBean(ExampleFilteredAutoConfiguration.class);
this.context.getBean(FILTERED);
}
@Configuration
@ComponentScan(basePackageClasses = ExampleConfiguration.class, excludeFilters = @ComponentScan.Filter(type = FilterType.CUSTOM, classes = TestAutoConfigurationExcludeFilter.class))
static class Config {
@ -71,10 +72,12 @@ public class AutoConfigurationExcludeFilterTests {
static class TestAutoConfigurationExcludeFilter
extends AutoConfigurationExcludeFilter {
@Override
protected List<String> getCandidateAutoConfigurations() {
return Collections.singletonList(ExampleFilteredAutoConfiguration.class.getName());
protected List<String> getAutoConfigurations() {
return Collections.singletonList(FILTERED.getName());
}
}
}

Loading…
Cancel
Save