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