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
This commit is contained in:
parent
47f801d535
commit
fb70a56c79
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* 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");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
|
@ -75,6 +75,8 @@ import org.springframework.core.io.support.SpringFactoriesLoader;
|
|||
@Import(EnableAutoConfigurationImportSelector.class)
|
||||
public @interface EnableAutoConfiguration {
|
||||
|
||||
String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";
|
||||
|
||||
/**
|
||||
* Exclude specific auto-configuration classes such that they will never be applied.
|
||||
* @return the classes to exclude
|
||||
|
|
|
|||
|
|
@ -64,6 +64,8 @@ public class EnableAutoConfigurationImportSelector
|
|||
implements DeferredImportSelector, BeanClassLoaderAware, ResourceLoaderAware,
|
||||
BeanFactoryAware, EnvironmentAware, Ordered {
|
||||
|
||||
private static final String[] NO_IMPORTS = {};
|
||||
|
||||
private ConfigurableListableBeanFactory beanFactory;
|
||||
|
||||
private Environment environment;
|
||||
|
|
@ -74,6 +76,9 @@ public class EnableAutoConfigurationImportSelector
|
|||
|
||||
@Override
|
||||
public String[] selectImports(AnnotationMetadata metadata) {
|
||||
if (!isEnabled(metadata)) {
|
||||
return NO_IMPORTS;
|
||||
}
|
||||
try {
|
||||
AnnotationAttributes attributes = getAttributes(metadata);
|
||||
List<String> configurations = getCandidateConfigurations(metadata,
|
||||
|
|
@ -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
|
||||
* {@link AnnotationMetadata}. By default this method will return attributes for
|
||||
|
|
|
|||
|
|
@ -150,11 +150,28 @@ public class EnableAutoConfigurationImportSelectorTests {
|
|||
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,
|
||||
String[] propertyExclusion) {
|
||||
given(this.annotationMetadata
|
||||
.getAnnotationAttributes(EnableAutoConfiguration.class.getName(), true))
|
||||
.willReturn(this.annotationAttributes);
|
||||
String annotationName = EnableAutoConfiguration.class.getName();
|
||||
given(this.annotationMetadata.isAnnotated(annotationName)).willReturn(true);
|
||||
given(this.annotationMetadata.getAnnotationAttributes(annotationName, true))
|
||||
.willReturn(this.annotationAttributes);
|
||||
given(this.annotationAttributes.getStringArray("exclude"))
|
||||
.willReturn(classExclusion);
|
||||
given(this.annotationAttributes.getStringArray("excludeName"))
|
||||
|
|
|
|||
Loading…
Reference in New Issue