Commit b75578d9 authored by Dave Syer's avatar Dave Syer

Adjust order of property sources

@PropertySources *can* and should be added in the slot
after the application.properties (code that is part of the
application should have lower precedence than external
configuration).

Fixes gh-1044
parent e81e9492
......@@ -207,11 +207,11 @@ sensible overriding of values, properties are considered in the the following or
. OS environment variables.
. JNDI attributes from `java:comp/env`
. A `RandomValuePropertySource` that only has properties in `random.*`.
. `@PropertySource` annotations on your `@Configuration` classes.
. Application properties outside of your packaged jar (`application.properties`
including YAML and profile variants).
. Application properties packaged inside your jar (`application.properties`
including YAML and profile variants).
. `@PropertySource` annotations on your `@Configuration` classes.
. Default properties (specified using `SpringApplication.setDefaultProperties`).
To provide a concrete example, suppose you develop a `@Component` that uses a
......
......@@ -497,19 +497,22 @@ public class ConfigFileApplicationListener implements
public static void finishAndRelocate(MutablePropertySources propertySources) {
ConfigurationPropertySources removed = (ConfigurationPropertySources) propertySources
.remove(ConfigurationPropertySources.NAME);
.get(ConfigurationPropertySources.NAME);
String name = ConfigurationPropertySources.NAME;
if (removed != null) {
for (PropertySource<?> propertySource : removed.sources) {
if (propertySource instanceof EnumerableCompositePropertySource) {
EnumerableCompositePropertySource composite = (EnumerableCompositePropertySource) propertySource;
for (PropertySource<?> nested : composite.getSource()) {
propertySources.addLast(nested);
propertySources.addAfter(name, nested);
name = nested.getName();
}
}
else {
propertySources.addLast(propertySource);
propertySources.addAfter(name, propertySource);
}
}
propertySources.remove(ConfigurationPropertySources.NAME);
}
}
......
......@@ -53,13 +53,23 @@ public class PropertySourcesBindingTests {
private Wrapper properties;
@Test
public void overridingOfPropertiesWorksAsExpected() {
public void overridingOfPropertiesOrderOfAtPropertySources() {
assertThat(this.properties.getBar(), is("override"));
}
@Test
public void overridingOfPropertiesAndBindToAtValue() {
assertThat(this.foo, is(this.properties.getFoo()));
}
@Test
public void overridingOfPropertiesOrderOfApplicationProperties() {
assertThat(this.properties.getFoo(), is("bucket"));
}
@Import({ SomeConfig.class })
@PropertySources({ @PropertySource("classpath:/override.properties"),
@PropertySource("classpath:/some.properties") })
@PropertySources({ @PropertySource("classpath:/some.properties"),
@PropertySource("classpath:/override.properties") })
@Configuration
@EnableConfigurationProperties(Wrapper.class)
public static class TestConfig {
......@@ -81,6 +91,16 @@ public class PropertySourcesBindingTests {
public static class Wrapper {
private String foo;
private String bar;
public String getBar() {
return this.bar;
}
public void setBar(String bar) {
this.bar = bar;
}
public String getFoo() {
return this.foo;
}
......
my.property=fromprofilepropertiesfile
the.property=fromprofilepropertiesfile
\ No newline at end of file
spring.profiles.active=myprofile
my.property=frompropertiesfile
the.property=frompropertiesfile
one.more=${my.property}
my.property=frommorepropertiesfile
the.property=frommorepropertiesfile
\ No newline at end of file
foo=spam
bar=some
\ No newline at end of file
my.property=fromspecificlocation
the.property=fromspecificlocation
\ No newline at end of file
my.property=frompropertiesfile
the.property=frompropertiesfile
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment