Commit fb70a56c authored by Phillip Webb's avatar Phillip Webb

Support override enable auto-configuration

Provide a way for full auto-configuration to be disabled
programmatically. Primarily added to allow special test annotations to
take over partial auto-configuration but still load
@SpringBootApplication classes.

See gh-4901
parent 47f801d5
/* /*
* Copyright 2012-2015 the original author or authors. * Copyright 2012-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -75,6 +75,8 @@ import org.springframework.core.io.support.SpringFactoriesLoader; ...@@ -75,6 +75,8 @@ import org.springframework.core.io.support.SpringFactoriesLoader;
@Import(EnableAutoConfigurationImportSelector.class) @Import(EnableAutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration { public @interface EnableAutoConfiguration {
String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";
/** /**
* Exclude specific auto-configuration classes such that they will never be applied. * Exclude specific auto-configuration classes such that they will never be applied.
* @return the classes to exclude * @return the classes to exclude
......
...@@ -64,6 +64,8 @@ public class EnableAutoConfigurationImportSelector ...@@ -64,6 +64,8 @@ public class EnableAutoConfigurationImportSelector
implements DeferredImportSelector, BeanClassLoaderAware, ResourceLoaderAware, implements DeferredImportSelector, BeanClassLoaderAware, ResourceLoaderAware,
BeanFactoryAware, EnvironmentAware, Ordered { BeanFactoryAware, EnvironmentAware, Ordered {
private static final String[] NO_IMPORTS = {};
private ConfigurableListableBeanFactory beanFactory; private ConfigurableListableBeanFactory beanFactory;
private Environment environment; private Environment environment;
...@@ -74,6 +76,9 @@ public class EnableAutoConfigurationImportSelector ...@@ -74,6 +76,9 @@ public class EnableAutoConfigurationImportSelector
@Override @Override
public String[] selectImports(AnnotationMetadata metadata) { public String[] selectImports(AnnotationMetadata metadata) {
if (!isEnabled(metadata)) {
return NO_IMPORTS;
}
try { try {
AnnotationAttributes attributes = getAttributes(metadata); AnnotationAttributes attributes = getAttributes(metadata);
List<String> configurations = getCandidateConfigurations(metadata, List<String> configurations = getCandidateConfigurations(metadata,
...@@ -90,6 +95,15 @@ public class EnableAutoConfigurationImportSelector ...@@ -90,6 +95,15 @@ public class EnableAutoConfigurationImportSelector
} }
} }
protected boolean isEnabled(AnnotationMetadata metadata) {
if (getClass().equals(EnableAutoConfigurationImportSelector.class)) {
return this.environment.getProperty(
EnableAutoConfiguration.ENABLED_OVERRIDE_PROPERTY, Boolean.class,
true);
}
return true;
}
/** /**
* Return the appropriate {@link AnnotationAttributes} from the * Return the appropriate {@link AnnotationAttributes} from the
* {@link AnnotationMetadata}. By default this method will return attributes for * {@link AnnotationMetadata}. By default this method will return attributes for
......
...@@ -150,11 +150,28 @@ public class EnableAutoConfigurationImportSelectorTests { ...@@ -150,11 +150,28 @@ public class EnableAutoConfigurationImportSelectorTests {
ThymeleafAutoConfiguration.class.getName()); ThymeleafAutoConfiguration.class.getName());
} }
@Test
public void propertyOverrideSetToTrue() throws Exception {
configureExclusions(new String[0], new String[0], new String[0]);
this.environment.setProperty(EnableAutoConfiguration.ENABLED_OVERRIDE_PROPERTY, "true");
String[] imports = this.importSelector.selectImports(this.annotationMetadata);
assertThat(imports).isNotEmpty();
}
@Test
public void propertyOverrideSetToFalse() throws Exception {
configureExclusions(new String[0], new String[0], new String[0]);
this.environment.setProperty(EnableAutoConfiguration.ENABLED_OVERRIDE_PROPERTY, "false");
String[] imports = this.importSelector.selectImports(this.annotationMetadata);
assertThat(imports).isEmpty();
}
private void configureExclusions(String[] classExclusion, String[] nameExclusion, private void configureExclusions(String[] classExclusion, String[] nameExclusion,
String[] propertyExclusion) { String[] propertyExclusion) {
given(this.annotationMetadata String annotationName = EnableAutoConfiguration.class.getName();
.getAnnotationAttributes(EnableAutoConfiguration.class.getName(), true)) given(this.annotationMetadata.isAnnotated(annotationName)).willReturn(true);
.willReturn(this.annotationAttributes); given(this.annotationMetadata.getAnnotationAttributes(annotationName, true))
.willReturn(this.annotationAttributes);
given(this.annotationAttributes.getStringArray("exclude")) given(this.annotationAttributes.getStringArray("exclude"))
.willReturn(classExclusion); .willReturn(classExclusion);
given(this.annotationAttributes.getStringArray("excludeName")) given(this.annotationAttributes.getStringArray("excludeName"))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment