Skip to content

Commit 8c6fe2c

Browse files
author
Phillip Webb
committed
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 (cherry picked from commit e3d3d8c)
1 parent 7a5a689 commit 8c6fe2c

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,8 +306,8 @@ private void processPropertySource(AnnotationAttributes propertySource) throws I
306306
}
307307
else {
308308
CompositePropertySource ps = new CompositePropertySource(name);
309-
for (String location : locations) {
310-
ps.addPropertySource(new ResourcePropertySource(location, classLoader));
309+
for (int i = locations.length - 1; i >= 0; i--) {
310+
ps.addPropertySource(new ResourcePropertySource(locations[i], classLoader));
311311
}
312312
this.propertySources.push(ps);
313313
}

spring-context/src/test/java/org/springframework/context/annotation/PropertySourceAnnotationTests.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,18 @@ public void withNameAndMultipleResourceLocations() {
130130
assertThat(ctx.getEnvironment().containsProperty("from.p2"), is(true));
131131
}
132132

133+
/**
134+
* SPR-10820
135+
*/
136+
@Test
137+
public void orderingWithAndWithoutNameAndMultipleResourceLocations() {
138+
// p2 should 'win' as it was registered last
139+
AnnotationConfigApplicationContext ctxWithName = new AnnotationConfigApplicationContext(ConfigWithNameAndMultipleResourceLocations.class);
140+
AnnotationConfigApplicationContext ctxWithoutName = new AnnotationConfigApplicationContext(ConfigWithMultipleResourceLocations.class);
141+
assertThat(ctxWithoutName.getEnvironment().getProperty("testbean.name"), equalTo("p2TestBean"));
142+
assertThat(ctxWithName.getEnvironment().getProperty("testbean.name"), equalTo("p2TestBean"));
143+
}
144+
133145
@Test(expected=IllegalArgumentException.class)
134146
public void withEmptyResourceLocations() {
135147
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
@@ -209,6 +221,15 @@ static class P2Config {
209221
static class ConfigWithNameAndMultipleResourceLocations {
210222
}
211223

224+
@Configuration
225+
@PropertySource(
226+
value = {
227+
"classpath:org/springframework/context/annotation/p1.properties",
228+
"classpath:org/springframework/context/annotation/p2.properties"
229+
})
230+
static class ConfigWithMultipleResourceLocations {
231+
}
232+
212233

213234
@Configuration
214235
@PropertySource(value = {})

0 commit comments

Comments
 (0)