Add config properties before default properties

Update ConfigFileApplicationListener so that configuration properties
are added before `defaultProperties` if they exist.

Fixes gh-4362
pull/4335/merge
Marcin Grzejszczak 9 years ago committed by Phillip Webb
parent 18db4f6c62
commit 19f141dc59

@ -569,9 +569,20 @@ public class ConfigFileApplicationListener implements EnvironmentPostProcessor,
for (PropertySource<?> item : sources) { for (PropertySource<?> item : sources) {
reorderedSources.add(item); reorderedSources.add(item);
} }
// Maybe we should add before the DEFAULT_PROPERTIES if it exists? addConfigurationProperties(
this.environment.getPropertySources() new ConfigurationPropertySources(reorderedSources));
.addLast(new ConfigurationPropertySources(reorderedSources)); }
private void addConfigurationProperties(
ConfigurationPropertySources configurationSources) {
MutablePropertySources existingSources = this.environment
.getPropertySources();
if (existingSources.contains(DEFAULT_PROPERTIES)) {
existingSources.addBefore(DEFAULT_PROPERTIES, configurationSources);
}
else {
existingSources.addLast(configurationSources);
}
} }
} }

@ -765,6 +765,18 @@ public class ConfigFileApplicationListenerTests {
assertThat(property, equalTo("false")); assertThat(property, equalTo("false"));
} }
@Test
public void addBeforeDefaultProperties() throws Exception {
MapPropertySource defaultSource = new MapPropertySource("defaultProperties",
Collections.<String, Object>singletonMap("the.property",
"fromdefaultproperties"));
this.environment.getPropertySources().addFirst(defaultSource);
this.initializer.setSearchNames("testproperties");
this.initializer.postProcessEnvironment(this.environment, this.application);
String property = this.environment.getProperty("the.property");
assertThat(property, equalTo("frompropertiesfile"));
}
private static Matcher<? super ConfigurableEnvironment> containsPropertySource( private static Matcher<? super ConfigurableEnvironment> containsPropertySource(
final String sourceName) { final String sourceName) {
return new TypeSafeDiagnosingMatcher<ConfigurableEnvironment>() { return new TypeSafeDiagnosingMatcher<ConfigurableEnvironment>() {

Loading…
Cancel
Save