Remove include from @EnableAutoConfiguration
Revert commit 1d31d23e29 to
remove `include` from `@EnableAutoConfiguration`. We'll
add a dedicated annotation instead to fix this.
See gh-3660
This commit is contained in:
@@ -75,15 +75,6 @@ import org.springframework.core.io.support.SpringFactoriesLoader;
|
||||
AutoConfigurationPackages.Registrar.class })
|
||||
public @interface EnableAutoConfiguration {
|
||||
|
||||
/**
|
||||
* Include only the specified auto-configuration classes and do not attempt full
|
||||
* auto-configuration. Using this attribute means that {@code spring.factories} files
|
||||
* will not be considered. This attribute should not generally be specified in
|
||||
* production applications, however, it is useful for tests.
|
||||
* @return the classes to include
|
||||
*/
|
||||
Class<?>[] include() default {};
|
||||
|
||||
/**
|
||||
* Exclude specific auto-configuration classes such that they will never be applied.
|
||||
* @return the classes to exclude
|
||||
|
||||
@@ -75,12 +75,14 @@ class EnableAutoConfigurationImportSelector implements DeferredImportSelector,
|
||||
+ " annotated with @EnableAutoConfiguration?");
|
||||
|
||||
// Find all possible auto configuration classes, filtering duplicates
|
||||
List<String> factories = getFactories(attributes);
|
||||
List<String> factories = new ArrayList<String>(new LinkedHashSet<String>(
|
||||
SpringFactoriesLoader.loadFactoryNames(EnableAutoConfiguration.class,
|
||||
this.beanClassLoader)));
|
||||
|
||||
// Remove those specifically excluded
|
||||
Set<String> excluded = new LinkedHashSet<String>();
|
||||
excluded.addAll(asList(attributes, "exclude"));
|
||||
excluded.addAll(asList(attributes, "excludeName"));
|
||||
excluded.addAll(Arrays.asList(attributes.getStringArray("exclude")));
|
||||
excluded.addAll(Arrays.asList(attributes.getStringArray("excludeName")));
|
||||
excluded.addAll(getExcludeAutoConfigurationsProperty());
|
||||
factories.removeAll(excluded);
|
||||
ConditionEvaluationReport.get(this.beanFactory).recordExclusions(excluded);
|
||||
@@ -98,20 +100,6 @@ class EnableAutoConfigurationImportSelector implements DeferredImportSelector,
|
||||
}
|
||||
}
|
||||
|
||||
private List<String> getFactories(AnnotationAttributes attributes) {
|
||||
List<String> factories = asList(attributes, "include");
|
||||
if (factories.isEmpty()) {
|
||||
factories = SpringFactoriesLoader.loadFactoryNames(
|
||||
EnableAutoConfiguration.class, this.beanClassLoader);
|
||||
}
|
||||
return new ArrayList<String>(new LinkedHashSet<String>(factories));
|
||||
}
|
||||
|
||||
private List<String> asList(AnnotationAttributes attributes, String name) {
|
||||
String[] value = attributes.getStringArray(name);
|
||||
return Arrays.asList(value == null ? new String[0] : value);
|
||||
}
|
||||
|
||||
private List<String> getExcludeAutoConfigurationsProperty() {
|
||||
RelaxedPropertyResolver resolver = new RelaxedPropertyResolver(this.environment,
|
||||
"spring.autoconfigure.");
|
||||
|
||||
@@ -148,18 +148,6 @@ public class EnableAutoConfigurationImportSelectorTests {
|
||||
ThymeleafAutoConfiguration.class.getName()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void classIncludesAreApplied() throws Exception {
|
||||
given(
|
||||
this.annotationMetadata.getAnnotationAttributes(
|
||||
EnableAutoConfiguration.class.getName(), true)).willReturn(
|
||||
this.annotationAttributes);
|
||||
given(this.annotationAttributes.getStringArray("include")).willReturn(
|
||||
new String[] { FreeMarkerAutoConfiguration.class.getName() });
|
||||
String[] imports = this.importSelector.selectImports(this.annotationMetadata);
|
||||
assertThat(imports.length, is(equalTo(1)));
|
||||
}
|
||||
|
||||
private void configureExclusions(String[] classExclusion, String[] nameExclusion,
|
||||
String[] propertyExclusion) {
|
||||
given(
|
||||
@@ -180,5 +168,4 @@ public class EnableAutoConfigurationImportSelectorTests {
|
||||
return SpringFactoriesLoader.loadFactoryNames(EnableAutoConfiguration.class,
|
||||
getClass().getClassLoader());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -18,11 +18,9 @@ package org.springframework.boot.autoconfigure.context;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.test.EnvironmentTestUtils;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import static org.hamcrest.core.Is.is;
|
||||
@@ -46,7 +44,8 @@ public class ConfigurationPropertiesAutoConfigurationTests {
|
||||
|
||||
@Test
|
||||
public void processAnnotatedBean() {
|
||||
load(new Class[] { AutoConfigured.class, SampleBean.class }, "foo.name:test");
|
||||
load(new Class[] { SampleBean.class,
|
||||
ConfigurationPropertiesAutoConfiguration.class }, "foo.name:test");
|
||||
assertThat(this.context.getBean(SampleBean.class).getName(), is("test"));
|
||||
}
|
||||
|
||||
@@ -63,12 +62,6 @@ public class ConfigurationPropertiesAutoConfigurationTests {
|
||||
this.context.refresh();
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableAutoConfiguration(include = ConfigurationPropertiesAutoConfiguration.class)
|
||||
static class AutoConfigured {
|
||||
|
||||
}
|
||||
|
||||
@Component
|
||||
@ConfigurationProperties("foo")
|
||||
static class SampleBean {
|
||||
|
||||
Reference in New Issue
Block a user