diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/EnableAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/EnableAutoConfiguration.java index aff3fcc3b23..810237d3ecd 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/EnableAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/EnableAutoConfiguration.java @@ -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 diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/EnableAutoConfigurationImportSelector.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/EnableAutoConfigurationImportSelector.java index 809631fe811..cdb012b4d1c 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/EnableAutoConfigurationImportSelector.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/EnableAutoConfigurationImportSelector.java @@ -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 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 diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/EnableAutoConfigurationImportSelectorTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/EnableAutoConfigurationImportSelectorTests.java index 2406ebb4749..b19a2c3d56b 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/EnableAutoConfigurationImportSelectorTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/EnableAutoConfigurationImportSelectorTests.java @@ -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"))