Fix `spring.application.exclude` YAML property

`spring.application.exclude` is retrieved via the
`RelaxedPropertyResolver` API explicitly and it does not have any
standard API to retrieve a list of values. As a consequence that property
could only be specified as a comma-separated value.

This felt convoluted in YAML. `RelaxedPropertyResolver` has now a
`getProperties` method that works with both comma-separated value and
index elements (i.e. list).

Closes gh-4352
This commit is contained in:
Stephane Nicoll 2015-10-30 17:23:40 +01:00
parent 718ea5f78b
commit abfd139d8f
5 changed files with 73 additions and 5 deletions

View File

@ -153,8 +153,12 @@ public class EnableAutoConfigurationImportSelector implements DeferredImportSele
private List<String> getExcludeAutoConfigurationsProperty() {
RelaxedPropertyResolver resolver = new RelaxedPropertyResolver(getEnvironment(),
"spring.autoconfigure.");
String[] exclude = resolver.getProperty("exclude", String[].class);
return (Arrays.asList(exclude == null ? new String[0] : exclude));
Collection<Object> raw = resolver.getProperties("exclude");
List<String> values = new ArrayList<String>();
for (Object r : raw) {
values.add(r.toString());
}
return values;
}
private List<String> sort(List<String> configurations) throws IOException {

View File

@ -45,7 +45,7 @@
},
{
"name": "spring.autoconfigure.exclude",
"type": "java.lang.Class[]",
"type": "java.util.List<java.lang.Class>",
"description": "Auto-configuration classes to exclude."
},
{

View File

@ -134,6 +134,21 @@ public class EnableAutoConfigurationImportSelectorTests {
VelocityAutoConfiguration.class.getName()));
}
@Test
public void severalPropertyYamlExclusionsAreApplied() {
configureExclusions(new String[0], new String[0], new String[0]);
this.environment.setProperty("spring.autoconfigure.exclude[0]",
FreeMarkerAutoConfiguration.class.getName());
this.environment.setProperty("spring.autoconfigure.exclude[1]",
VelocityAutoConfiguration.class.getName());
String[] imports = this.importSelector.selectImports(this.annotationMetadata);
assertThat(imports.length,
is(equalTo(getAutoConfigurationClassNames().size() - 2)));
assertThat(ConditionEvaluationReport.get(this.beanFactory).getExclusions(),
containsInAnyOrder(FreeMarkerAutoConfiguration.class.getName(),
VelocityAutoConfiguration.class.getName()));
}
@Test
public void combinedExclusionsAreApplied() {
configureExclusions(new String[] { VelocityAutoConfiguration.class.getName() },

View File

@ -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.
@ -16,6 +16,9 @@
package org.springframework.boot.bind;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.springframework.core.env.ConfigurableEnvironment;
@ -128,6 +131,21 @@ public class RelaxedPropertyResolver implements PropertyResolver {
"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
* specified key. NOTE: this method can only be used if the underlying resolver is a

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2013 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.
@ -17,6 +17,7 @@
package org.springframework.boot.bind;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
@ -30,6 +31,7 @@ import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.StandardEnvironment;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertEquals;
@ -39,6 +41,7 @@ import static org.junit.Assert.assertThat;
* Tests for {@link RelaxedPropertyResolver}.
*
* @author Phillip Webb
* @author Stephane Nicoll
*/
public class RelaxedPropertyResolverTests {
@ -166,6 +169,34 @@ public class RelaxedPropertyResolverTests {
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
public void subProperties() throws Exception {
this.source.put("x.y.my-sub.a.b", "1");