From 19f141dc5906555e6d74160ab452abc50eabf14f Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Sun, 1 Nov 2015 11:30:38 +0100 Subject: [PATCH] Add config properties before default properties Update ConfigFileApplicationListener so that configuration properties are added before `defaultProperties` if they exist. Fixes gh-4362 --- .../config/ConfigFileApplicationListener.java | 17 ++++++++++++++--- .../ConfigFileApplicationListenerTests.java | 12 ++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java b/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java index 554ef0958c..e8389745f6 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java @@ -569,9 +569,20 @@ public class ConfigFileApplicationListener implements EnvironmentPostProcessor, for (PropertySource item : sources) { reorderedSources.add(item); } - // Maybe we should add before the DEFAULT_PROPERTIES if it exists? - this.environment.getPropertySources() - .addLast(new ConfigurationPropertySources(reorderedSources)); + addConfigurationProperties( + 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); + } } } diff --git a/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java b/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java index 3125882792..d15c8d558c 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java @@ -765,6 +765,18 @@ public class ConfigFileApplicationListenerTests { assertThat(property, equalTo("false")); } + @Test + public void addBeforeDefaultProperties() throws Exception { + MapPropertySource defaultSource = new MapPropertySource("defaultProperties", + Collections.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 containsPropertySource( final String sourceName) { return new TypeSafeDiagnosingMatcher() {