Skip to content

Commit

Permalink
Add excludeName to EnableAutoConfiguration
Browse files Browse the repository at this point in the history
Allow user to exclude an auto-configuration class by specifying the fully
qualified name instead of the class reference.

Closes spring-projectsgh-2660
  • Loading branch information
snicoll committed May 27, 2015
1 parent fc61f2e commit a6f671b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -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");
* you may not use this file except in compliance with the License.
Expand All @@ -20,7 +20,6 @@
import org.junit.Test;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
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.builder.SpringApplicationBuilder;
import org.springframework.boot.test.ApplicationContextTestUtils;
Expand Down Expand Up @@ -53,17 +52,18 @@ public void testChild() {
"--server.port=0");
}

@EnableAutoConfiguration(exclude = { ElasticsearchAutoConfiguration.class,
@EnableAutoConfiguration(exclude = {
ElasticsearchDataAutoConfiguration.class,
ElasticsearchRepositoriesAutoConfiguration.class })
ElasticsearchRepositoriesAutoConfiguration.class},
excludeName = {"org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchAutoConfiguration"})
public static class Child {
}

@EnableAutoConfiguration(exclude = { JolokiaAutoConfiguration.class,
@EnableAutoConfiguration(exclude = {JolokiaAutoConfiguration.class,
EndpointMBeanExportAutoConfiguration.class,
ElasticsearchAutoConfiguration.class,
ElasticsearchDataAutoConfiguration.class,
ElasticsearchRepositoriesAutoConfiguration.class })
ElasticsearchRepositoriesAutoConfiguration.class},
excludeName = {"org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchAutoConfiguration"})
public static class Parent {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@
* <p>
* 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
* configuration that you never want to apply. Auto-configuration is always applied after
* user-defined beans have been registered.
* configuration that you never want to apply (use {@link #excludeName()} if you don't
* have access to them). Auto-configuration is always applied after user-defined beans
* have been registered.
* <p>
* 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
Expand All @@ -59,6 +60,7 @@
* {@link ConditionalOnMissingBean @ConditionalOnMissingBean} annotations).
*
* @author Phillip Webb
* @author Stephane Nicoll
* @see ConditionalOnBean
* @see ConditionalOnMissingBean
* @see ConditionalOnClass
Expand All @@ -78,4 +80,11 @@
*/
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 {};

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
*
* @author Phillip Webb
* @author Andy Wilkinson
* @author Stephane Nicoll
* @see EnableAutoConfiguration
*/
@Order(Ordered.LOWEST_PRECEDENCE)
Expand Down Expand Up @@ -73,9 +74,8 @@ public String[] selectImports(AnnotationMetadata metadata) {
this.beanClassLoader)));

// Remove those specifically disabled
List<String> excluded = Arrays.asList(attributes.getStringArray("exclude"));
factories.removeAll(excluded);
ConditionEvaluationReport.get(this.beanFactory).recordExclusions(excluded);
exclude(Arrays.asList(attributes.getStringArray("exclude")), factories);
exclude(Arrays.asList(attributes.getStringArray("excludeName")), factories);

// Sort
factories = new AutoConfigurationSorter(this.resourceLoader)
Expand All @@ -88,6 +88,11 @@ public String[] selectImports(AnnotationMetadata metadata) {
}
}

private void exclude(List<String> excluded, List<String> factories) {
factories.removeAll(excluded);
ConditionEvaluationReport.get(this.beanFactory).recordExclusions(excluded);
}

@Override
public void setBeanClassLoader(ClassLoader classLoader) {
this.beanClassLoader = classLoader;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
* {@code @EnableAutoConfiguration} and {@code @ComponentScan}.
*
* @author Phillip Webb
* @author Stephane Nicoll
* @since 1.2.0
*/
@Target(ElementType.TYPE)
Expand All @@ -52,4 +53,11 @@
*/
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 {};

}

0 comments on commit a6f671b

Please sign in to comment.