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");
|
* 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;
|
||||||
@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
|
||||||
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
|
||||||
|
|
||||||
@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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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,10 +150,27 @@ 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);
|
||||||
|
given(this.annotationMetadata.getAnnotationAttributes(annotationName, true))
|
||||||
.willReturn(this.annotationAttributes);
|
.willReturn(this.annotationAttributes);
|
||||||
given(this.annotationAttributes.getStringArray("exclude"))
|
given(this.annotationAttributes.getStringArray("exclude"))
|
||||||
.willReturn(classExclusion);
|
.willReturn(classExclusion);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue