From a6f671be3ed4ce9d70c8c5ffc76b8130bbbcc424 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Wed, 27 May 2015 14:24:47 +0200 Subject: [PATCH] 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 --- .../SpringApplicationHierarchyTests.java | 14 +++++++------- .../autoconfigure/EnableAutoConfiguration.java | 13 +++++++++++-- .../EnableAutoConfigurationImportSelector.java | 11 ++++++++--- .../boot/autoconfigure/SpringBootApplication.java | 8 ++++++++ 4 files changed, 34 insertions(+), 12 deletions(-) diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/SpringApplicationHierarchyTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/SpringApplicationHierarchyTests.java index 61f6a7b54f..85c895c552 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/SpringApplicationHierarchyTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/SpringApplicationHierarchyTests.java @@ -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 { } 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 b042821a1b..e1a5b07a6f 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 @@ -43,8 +43,9 @@ import org.springframework.core.io.support.SpringFactoriesLoader; *

* 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. *

* 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 {}; + } 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 7388d0cdc1..633faa27b4 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 @@ -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 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 excluded, List factories) { + factories.removeAll(excluded); + ConditionEvaluationReport.get(this.beanFactory).recordExclusions(excluded); + } + @Override public void setBeanClassLoader(ClassLoader classLoader) { this.beanClassLoader = classLoader; diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/SpringBootApplication.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/SpringBootApplication.java index 34f31b435e..250bb49343 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/SpringBootApplication.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/SpringBootApplication.java @@ -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 {}; + }