提交 e3d3d8cd 编写于 作者: P Phillip Webb

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
上级 b0ff834e
...@@ -320,8 +320,8 @@ class ConfigurationClassParser { ...@@ -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 { ...@@ -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 { ...@@ -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 = {})
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册