Add excludeName to EnableAutoConfiguration

Allow user to exclude an auto-configuration class by specifying the fully
qualified name instead of the class reference.

Closes gh-2660
pull/3050/head
Stephane Nicoll 10 years ago
parent fc61f2e837
commit a6f671be3e

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2014 the original author or authors. * Copyright 2012-2015 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.
@ -20,7 +20,6 @@ import org.junit.After;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchRepositoriesAutoConfiguration; import org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchRepositoriesAutoConfiguration;
import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchAutoConfiguration;
import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchDataAutoConfiguration; import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchDataAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.test.ApplicationContextTestUtils; import org.springframework.boot.test.ApplicationContextTestUtils;
@ -53,17 +52,18 @@ public class SpringApplicationHierarchyTests {
"--server.port=0"); "--server.port=0");
} }
@EnableAutoConfiguration(exclude = { ElasticsearchAutoConfiguration.class, @EnableAutoConfiguration(exclude = {
ElasticsearchDataAutoConfiguration.class, ElasticsearchDataAutoConfiguration.class,
ElasticsearchRepositoriesAutoConfiguration.class }) ElasticsearchRepositoriesAutoConfiguration.class},
excludeName = {"org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchAutoConfiguration"})
public static class Child { public static class Child {
} }
@EnableAutoConfiguration(exclude = { JolokiaAutoConfiguration.class, @EnableAutoConfiguration(exclude = {JolokiaAutoConfiguration.class,
EndpointMBeanExportAutoConfiguration.class, EndpointMBeanExportAutoConfiguration.class,
ElasticsearchAutoConfiguration.class,
ElasticsearchDataAutoConfiguration.class, ElasticsearchDataAutoConfiguration.class,
ElasticsearchRepositoriesAutoConfiguration.class }) ElasticsearchRepositoriesAutoConfiguration.class},
excludeName = {"org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchAutoConfiguration"})
public static class Parent { public static class Parent {
} }

@ -43,8 +43,9 @@ import org.springframework.core.io.support.SpringFactoriesLoader;
* <p> * <p>
* Auto-configuration tries to be as intelligent as possible and will back-away as you * Auto-configuration tries to be as intelligent as possible and will back-away as you
* define more of your own configuration. You can always manually {@link #exclude()} any * define more of your own configuration. You can always manually {@link #exclude()} any
* configuration that you never want to apply. Auto-configuration is always applied after * configuration that you never want to apply (use {@link #excludeName()} if you don't
* user-defined beans have been registered. * have access to them). Auto-configuration is always applied after user-defined beans
* have been registered.
* <p> * <p>
* The package of the class that is annotated with {@code @EnableAutoConfiguration} has * The package of the class that is annotated with {@code @EnableAutoConfiguration} has
* specific significance and is often used as a 'default'. For example, it will be used * specific significance and is often used as a 'default'. For example, it will be used
@ -59,6 +60,7 @@ import org.springframework.core.io.support.SpringFactoriesLoader;
* {@link ConditionalOnMissingBean @ConditionalOnMissingBean} annotations). * {@link ConditionalOnMissingBean @ConditionalOnMissingBean} annotations).
* *
* @author Phillip Webb * @author Phillip Webb
* @author Stephane Nicoll
* @see ConditionalOnBean * @see ConditionalOnBean
* @see ConditionalOnMissingBean * @see ConditionalOnMissingBean
* @see ConditionalOnClass * @see ConditionalOnClass
@ -78,4 +80,11 @@ public @interface EnableAutoConfiguration {
*/ */
Class<?>[] exclude() default {}; Class<?>[] exclude() default {};
/**
* Exclude specific auto-configuration class names such that they will never be
* applied.
* @return the class names to exclude
*/
String[] excludeName() default {};
} }

@ -44,6 +44,7 @@ import org.springframework.util.Assert;
* *
* @author Phillip Webb * @author Phillip Webb
* @author Andy Wilkinson * @author Andy Wilkinson
* @author Stephane Nicoll
* @see EnableAutoConfiguration * @see EnableAutoConfiguration
*/ */
@Order(Ordered.LOWEST_PRECEDENCE) @Order(Ordered.LOWEST_PRECEDENCE)
@ -73,9 +74,8 @@ class EnableAutoConfigurationImportSelector implements DeferredImportSelector,
this.beanClassLoader))); this.beanClassLoader)));
// Remove those specifically disabled // Remove those specifically disabled
List<String> excluded = Arrays.asList(attributes.getStringArray("exclude")); exclude(Arrays.asList(attributes.getStringArray("exclude")), factories);
factories.removeAll(excluded); exclude(Arrays.asList(attributes.getStringArray("excludeName")), factories);
ConditionEvaluationReport.get(this.beanFactory).recordExclusions(excluded);
// Sort // Sort
factories = new AutoConfigurationSorter(this.resourceLoader) factories = new AutoConfigurationSorter(this.resourceLoader)
@ -88,6 +88,11 @@ class EnableAutoConfigurationImportSelector implements DeferredImportSelector,
} }
} }
private void exclude(List<String> excluded, List<String> factories) {
factories.removeAll(excluded);
ConditionEvaluationReport.get(this.beanFactory).recordExclusions(excluded);
}
@Override @Override
public void setBeanClassLoader(ClassLoader classLoader) { public void setBeanClassLoader(ClassLoader classLoader) {
this.beanClassLoader = classLoader; this.beanClassLoader = classLoader;

@ -35,6 +35,7 @@ import org.springframework.context.annotation.Configuration;
* {@code @EnableAutoConfiguration} and {@code @ComponentScan}. * {@code @EnableAutoConfiguration} and {@code @ComponentScan}.
* *
* @author Phillip Webb * @author Phillip Webb
* @author Stephane Nicoll
* @since 1.2.0 * @since 1.2.0
*/ */
@Target(ElementType.TYPE) @Target(ElementType.TYPE)
@ -52,4 +53,11 @@ public @interface SpringBootApplication {
*/ */
Class<?>[] exclude() default {}; Class<?>[] exclude() default {};
/**
* Exclude specific auto-configuration class names such that they will never be
* applied.
* @return the class names to exclude
*/
String[] excludeName() default {};
} }

Loading…
Cancel
Save