From 55f3ca0172c56af9f71164ed3d0a057beacc85f2 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Tue, 27 May 2014 12:09:08 +0100 Subject: [PATCH] Remove misplaced re-ordering of default property source The early re-ordering (and in particular the temporary remove of the default properties) seemed to be a relic of an older approach that is no longer there since we refactored to support more sane profile ordering. Removing it doesn't seem to break anything and it allows you to specify the config file locations in SpringApplicationBuilder.properties(). Fixes gh-953, fixes gh-920 --- .../config/ConfigFileApplicationListener.java | 6 +--- .../ConfigFileApplicationListenerTests.java | 29 +++++++++++++++++++ .../src/test/resources/application.properties | 1 + 3 files changed, 31 insertions(+), 5 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 bf45646b26..3aaad79a91 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 @@ -152,12 +152,7 @@ public class ConfigFileApplicationListener implements ResourceLoader resourceLoader) { RandomValuePropertySource.addToEnvironment(environment); try { - PropertySource defaultProperties = environment.getPropertySources() - .remove(DEFAULT_PROPERTIES); new Loader(environment, resourceLoader).load(); - if (defaultProperties != null) { - environment.getPropertySources().addLast(defaultProperties); - } } catch (IOException ex) { throw new IllegalStateException("Unable to load configuration files", ex); @@ -456,6 +451,7 @@ public class ConfigFileApplicationListener implements 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)); } 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 5c3a10075d..38d2043a2e 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 @@ -23,6 +23,7 @@ import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.Collections; import java.util.List; import java.util.Properties; @@ -43,6 +44,7 @@ import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; import org.springframework.context.annotation.PropertySource; import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.core.env.MapPropertySource; import org.springframework.core.env.MutablePropertySources; import org.springframework.core.env.SimpleCommandLinePropertySource; import org.springframework.core.env.StandardEnvironment; @@ -260,6 +262,33 @@ public class ConfigFileApplicationListenerTests { assertThat(property, equalTo("fromsystem")); } + @Test + public void defaultPropertyAsFallback() throws Exception { + this.event + .getEnvironment() + .getPropertySources() + .addLast( + new MapPropertySource("defaultProperties", Collections + .singletonMap("my.fallback", (Object) "foo"))); + this.initializer.onApplicationEvent(this.event); + String property = this.environment.getProperty("my.fallback"); + assertThat(property, equalTo("foo")); + } + + @Test + public void defaultPropertyAsFallbackDuringFileParsing() throws Exception { + this.event + .getEnvironment() + .getPropertySources() + .addLast( + new MapPropertySource("defaultProperties", Collections + .singletonMap("spring.config.name", + (Object) "testproperties"))); + this.initializer.onApplicationEvent(this.event); + String property = this.environment.getProperty("my.property"); + assertThat(property, equalTo("frompropertiesfile")); + } + @Test public void loadPropertiesThenProfilePropertiesActivatedInSpringApplication() throws Exception { diff --git a/spring-boot/src/test/resources/application.properties b/spring-boot/src/test/resources/application.properties index 7cc1ee4906..2f0172e0f2 100644 --- a/spring-boot/src/test/resources/application.properties +++ b/spring-boot/src/test/resources/application.properties @@ -2,3 +2,4 @@ foo: bucket value: 1234 my.property: fromapplicationproperties sample.app.test.prop: * +my.placeholder: ${my.fallback}