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
This commit is contained in:
		
							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");
 | 
			
		||||
 * 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.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;
 | 
			
		||||
| 
						 | 
				
			
			@ -53,17 +52,18 @@ public class SpringApplicationHierarchyTests {
 | 
			
		|||
				"--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 {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -43,8 +43,9 @@ import org.springframework.core.io.support.SpringFactoriesLoader;
 | 
			
		|||
 * <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
 | 
			
		||||
| 
						 | 
				
			
			@ -59,6 +60,7 @@ import org.springframework.core.io.support.SpringFactoriesLoader;
 | 
			
		|||
 * {@link ConditionalOnMissingBean @ConditionalOnMissingBean} annotations).
 | 
			
		||||
 *
 | 
			
		||||
 * @author Phillip Webb
 | 
			
		||||
 * @author Stephane Nicoll
 | 
			
		||||
 * @see ConditionalOnBean
 | 
			
		||||
 * @see ConditionalOnMissingBean
 | 
			
		||||
 * @see ConditionalOnClass
 | 
			
		||||
| 
						 | 
				
			
			@ -78,4 +80,11 @@ public @interface EnableAutoConfiguration {
 | 
			
		|||
	 */
 | 
			
		||||
	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 Andy Wilkinson
 | 
			
		||||
 * @author Stephane Nicoll
 | 
			
		||||
 * @see EnableAutoConfiguration
 | 
			
		||||
 */
 | 
			
		||||
@Order(Ordered.LOWEST_PRECEDENCE)
 | 
			
		||||
| 
						 | 
				
			
			@ -73,9 +74,8 @@ class EnableAutoConfigurationImportSelector implements DeferredImportSelector,
 | 
			
		|||
							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)
 | 
			
		||||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
	public void setBeanClassLoader(ClassLoader classLoader) {
 | 
			
		||||
		this.beanClassLoader = classLoader;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -35,6 +35,7 @@ import org.springframework.context.annotation.Configuration;
 | 
			
		|||
 * {@code @EnableAutoConfiguration} and {@code @ComponentScan}.
 | 
			
		||||
 *
 | 
			
		||||
 * @author Phillip Webb
 | 
			
		||||
 * @author Stephane Nicoll
 | 
			
		||||
 * @since 1.2.0
 | 
			
		||||
 */
 | 
			
		||||
@Target(ElementType.TYPE)
 | 
			
		||||
| 
						 | 
				
			
			@ -52,4 +53,11 @@ public @interface SpringBootApplication {
 | 
			
		|||
	 */
 | 
			
		||||
	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…
	
		Reference in New Issue