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
This commit is contained in:
Dave Syer 2014-05-27 12:09:08 +01:00
parent 3c1815ef43
commit 55f3ca0172
3 changed files with 31 additions and 5 deletions

View File

@ -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));
}

View File

@ -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 {

View File

@ -2,3 +2,4 @@ foo: bucket
value: 1234
my.property: fromapplicationproperties
sample.app.test.prop: *
my.placeholder: ${my.fallback}