Consistent ordering for @PropertySource locations
Ensure that property source locations are processed in the same order regardless if the 'name' attribute is set or not. Prior to this commit multiple locations from a `@PropertySource` with a name were added to a `CompositePropertySource` in such a way that the first location would take precedence. This has now been reversed for consistence with unnamed `@PropertySource`s Issue: SPR-10820
This commit is contained in:
parent
b0ff834ee3
commit
e3d3d8cd95
|
|
@ -320,8 +320,8 @@ class ConfigurationClassParser {
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
CompositePropertySource ps = new CompositePropertySource(name);
|
CompositePropertySource ps = new CompositePropertySource(name);
|
||||||
for (String location : locations) {
|
for (int i = locations.length - 1; i >= 0; i--) {
|
||||||
ps.addPropertySource(new ResourcePropertySource(location, classLoader));
|
ps.addPropertySource(new ResourcePropertySource(locations[i], classLoader));
|
||||||
}
|
}
|
||||||
this.propertySources.push(ps);
|
this.propertySources.push(ps);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -130,6 +130,18 @@ public class PropertySourceAnnotationTests {
|
||||||
assertThat(ctx.getEnvironment().containsProperty("from.p2"), is(true));
|
assertThat(ctx.getEnvironment().containsProperty("from.p2"), is(true));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* SPR-10820
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void orderingWithAndWithoutNameAndMultipleResourceLocations() {
|
||||||
|
// p2 should 'win' as it was registered last
|
||||||
|
AnnotationConfigApplicationContext ctxWithName = new AnnotationConfigApplicationContext(ConfigWithNameAndMultipleResourceLocations.class);
|
||||||
|
AnnotationConfigApplicationContext ctxWithoutName = new AnnotationConfigApplicationContext(ConfigWithMultipleResourceLocations.class);
|
||||||
|
assertThat(ctxWithoutName.getEnvironment().getProperty("testbean.name"), equalTo("p2TestBean"));
|
||||||
|
assertThat(ctxWithName.getEnvironment().getProperty("testbean.name"), equalTo("p2TestBean"));
|
||||||
|
}
|
||||||
|
|
||||||
@Test(expected=IllegalArgumentException.class)
|
@Test(expected=IllegalArgumentException.class)
|
||||||
public void withEmptyResourceLocations() {
|
public void withEmptyResourceLocations() {
|
||||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||||
|
|
@ -209,6 +221,15 @@ public class PropertySourceAnnotationTests {
|
||||||
static class ConfigWithNameAndMultipleResourceLocations {
|
static class ConfigWithNameAndMultipleResourceLocations {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@PropertySource(
|
||||||
|
value = {
|
||||||
|
"classpath:org/springframework/context/annotation/p1.properties",
|
||||||
|
"classpath:org/springframework/context/annotation/p2.properties"
|
||||||
|
})
|
||||||
|
static class ConfigWithMultipleResourceLocations {
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@PropertySource(value = {})
|
@PropertySource(value = {})
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue