Commit 0ccd3370 authored by Phillip Webb's avatar Phillip Webb

Use RelaxedDataBinder for excludes

Update `EnableAutoConfigurationImportSelector` to directly use the
RelaxedDataBinder when obtaining excludes. This removes the need for
the additional getProperties method on RelaxedPropertyResolver.

See gh-4352
parent abfd139d
...@@ -30,6 +30,8 @@ import org.springframework.beans.factory.BeanFactory; ...@@ -30,6 +30,8 @@ import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware; import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.boot.autoconfigure.condition.ConditionEvaluationReport; import org.springframework.boot.autoconfigure.condition.ConditionEvaluationReport;
import org.springframework.boot.bind.PropertySourcesPropertyValues;
import org.springframework.boot.bind.RelaxedDataBinder;
import org.springframework.boot.bind.RelaxedPropertyResolver; import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.context.EnvironmentAware; import org.springframework.context.EnvironmentAware;
import org.springframework.context.ResourceLoaderAware; import org.springframework.context.ResourceLoaderAware;
...@@ -37,6 +39,7 @@ import org.springframework.context.annotation.DeferredImportSelector; ...@@ -37,6 +39,7 @@ import org.springframework.context.annotation.DeferredImportSelector;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.core.annotation.AnnotationAttributes; import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.annotation.Order; import org.springframework.core.annotation.Order;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.core.io.ResourceLoader; import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.SpringFactoriesLoader; import org.springframework.core.io.support.SpringFactoriesLoader;
...@@ -151,14 +154,18 @@ public class EnableAutoConfigurationImportSelector implements DeferredImportSele ...@@ -151,14 +154,18 @@ public class EnableAutoConfigurationImportSelector implements DeferredImportSele
} }
private List<String> getExcludeAutoConfigurationsProperty() { private List<String> getExcludeAutoConfigurationsProperty() {
if (getEnvironment() instanceof ConfigurableEnvironment) {
Excludes excludes = new Excludes();
RelaxedDataBinder binder = new RelaxedDataBinder(excludes,
"spring.autoconfigure.");
binder.bind(new PropertySourcesPropertyValues(
((ConfigurableEnvironment) getEnvironment()).getPropertySources()));
return excludes.getExclude();
}
RelaxedPropertyResolver resolver = new RelaxedPropertyResolver(getEnvironment(), RelaxedPropertyResolver resolver = new RelaxedPropertyResolver(getEnvironment(),
"spring.autoconfigure."); "spring.autoconfigure.");
Collection<Object> raw = resolver.getProperties("exclude"); String[] exclude = resolver.getProperty("exclude", String[].class);
List<String> values = new ArrayList<String>(); return (Arrays.asList(exclude == null ? new String[0] : exclude));
for (Object r : raw) {
values.add(r.toString());
}
return values;
} }
private List<String> sort(List<String> configurations) throws IOException { private List<String> sort(List<String> configurations) throws IOException {
...@@ -221,4 +228,21 @@ public class EnableAutoConfigurationImportSelector implements DeferredImportSele ...@@ -221,4 +228,21 @@ public class EnableAutoConfigurationImportSelector implements DeferredImportSele
return this.resourceLoader; return this.resourceLoader;
} }
/**
* Bindable object used to get excludes.
*/
static class Excludes {
private List<String> exclude = new ArrayList<String>();
public List<String> getExclude() {
return this.exclude;
}
public void setExclude(List<String> excludes) {
this.exclude = excludes;
}
}
} }
...@@ -16,9 +16,6 @@ ...@@ -16,9 +16,6 @@
package org.springframework.boot.bind; package org.springframework.boot.bind;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map; import java.util.Map;
import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.ConfigurableEnvironment;
...@@ -131,21 +128,6 @@ public class RelaxedPropertyResolver implements PropertyResolver { ...@@ -131,21 +128,6 @@ public class RelaxedPropertyResolver implements PropertyResolver {
"Unable to resolve placeholders with relaxed properties"); "Unable to resolve placeholders with relaxed properties");
} }
/**
* Return the property values associated with the given key, or an empty
* list if the key cannot be resolved.
* @param key the property name to resolve
* @return the property values for that key
*/
public List<Object> getProperties(String key) {
Object[] singular = getProperty(key, Object[].class);
if (singular != null) {
return Arrays.asList(singular);
}
Map<String, Object> subProperties = getSubProperties(key);
return new ArrayList<Object>(subProperties.values());
}
/** /**
* Return a Map of all values from all underlying properties that start with the * Return a Map of all values from all underlying properties that start with the
* specified key. NOTE: this method can only be used if the underlying resolver is a * specified key. NOTE: this method can only be used if the underlying resolver is a
......
...@@ -17,7 +17,6 @@ ...@@ -17,7 +17,6 @@
package org.springframework.boot.bind; package org.springframework.boot.bind;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
...@@ -31,7 +30,6 @@ import org.springframework.core.env.MutablePropertySources; ...@@ -31,7 +30,6 @@ import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertiesPropertySource; import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.StandardEnvironment; import org.springframework.core.env.StandardEnvironment;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.nullValue; import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
...@@ -169,34 +167,6 @@ public class RelaxedPropertyResolverTests { ...@@ -169,34 +167,6 @@ public class RelaxedPropertyResolverTests {
assertThat(this.resolver.getProperty("foo-bar"), equalTo("spam")); assertThat(this.resolver.getProperty("foo-bar"), equalTo("spam"));
} }
@Test
public void commaSeparatedProperties() throws Exception {
this.source.put("x.y.foo", "1,2");
this.resolver = new RelaxedPropertyResolver(this.environment, "x.y.");
List<Object> properties = this.resolver.getProperties("foo");
assertThat(properties.size(), equalTo(2));
assertThat(properties, contains((Object) "1", (Object) "2"));
}
@Test
public void commaSeparatedPropertiesSingleValue() throws Exception {
this.source.put("x.y.foo", "1");
this.resolver = new RelaxedPropertyResolver(this.environment, "x.y.");
List<Object> properties = this.resolver.getProperties("foo");
assertThat(properties.size(), equalTo(1));
assertThat(properties, contains((Object) "1"));
}
@Test
public void indexedProperties() throws Exception {
this.source.put("x.y.foo[0]", "1");
this.source.put("x.y.foo[1]", "2");
this.resolver = new RelaxedPropertyResolver(this.environment, "x.y.");
List<Object> properties = this.resolver.getProperties("foo");
assertThat(properties.size(), equalTo(2));
assertThat(properties, contains((Object) "1", (Object) "2"));
}
@Test @Test
public void subProperties() throws Exception { public void subProperties() throws Exception {
this.source.put("x.y.my-sub.a.b", "1"); this.source.put("x.y.my-sub.a.b", "1");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment