Skip to content

Commit 90b93ff

Browse files
committed
Restored property source ordering (fixing regression from 4.0.x)
Issue: SPR-12198
1 parent f4219ca commit 90b93ff

File tree

4 files changed

+25
-5
lines changed

4 files changed

+25
-5
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,8 +375,7 @@ private void addPropertySource(ResourcePropertySource propertySource) {
375375
propertySources.addLast(propertySource);
376376
}
377377
else {
378-
String firstProcessed = this.propertySourceNames.iterator().next();
379-
propertySources.addBefore(firstProcessed, propertySource);
378+
propertySources.addFirst(propertySource);
380379
}
381380
}
382381
this.propertySourceNames.add(name);

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

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,13 @@ public void withSameSourceImportedInDifferentOrder() throws Exception {
229229
assertThat(ctx.getEnvironment().getProperty("testbean.name"), equalTo("p2TestBean"));
230230
}
231231

232+
@Test
233+
public void orderingWithAndWithoutNameAndFourResourceLocations() {
234+
// SPR-12198: p4 should 'win' as it was registered last
235+
AnnotationConfigApplicationContext ctxWithoutName = new AnnotationConfigApplicationContext(ConfigWithFourResourceLocations.class);
236+
assertThat(ctxWithoutName.getEnvironment().getProperty("testbean.name"), equalTo("p4TestBean"));
237+
}
238+
232239

233240
@Configuration
234241
@PropertySource(value="classpath:${unresolvable}/p1.properties")
@@ -367,8 +374,8 @@ static class ConfigWithPropertySources {
367374

368375
@Configuration
369376
@PropertySources({
370-
@PropertySource(name = "psName", value = "classpath:org/springframework/context/annotation/p1.properties"),
371-
@PropertySource(name = "psName", value = "classpath:org/springframework/context/annotation/p2.properties"),
377+
@PropertySource(name = "psName", value = "classpath:org/springframework/context/annotation/p1.properties"),
378+
@PropertySource(name = "psName", value = "classpath:org/springframework/context/annotation/p2.properties"),
372379
})
373380
static class ConfigWithNamedPropertySources {
374381
}
@@ -401,7 +408,7 @@ static class ConfigWithEmptyResourceLocations {
401408
}
402409

403410

404-
@Import({ ConfigImportedWithSameSourceImportedInDifferentOrder.class })
411+
@Import(ConfigImportedWithSameSourceImportedInDifferentOrder.class)
405412
@PropertySources({
406413
@PropertySource("classpath:org/springframework/context/annotation/p1.properties"),
407414
@PropertySource("classpath:org/springframework/context/annotation/p2.properties")
@@ -420,4 +427,16 @@ public static class ConfigWithSameSourceImportedInDifferentOrder {
420427
public static class ConfigImportedWithSameSourceImportedInDifferentOrder {
421428
}
422429

430+
431+
@Configuration
432+
@PropertySource(
433+
value = {
434+
"classpath:org/springframework/context/annotation/p1.properties",
435+
"classpath:org/springframework/context/annotation/p2.properties",
436+
"classpath:org/springframework/context/annotation/p3.properties",
437+
"classpath:org/springframework/context/annotation/p4.properties"
438+
})
439+
static class ConfigWithFourResourceLocations {
440+
}
441+
423442
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
testbean.name=p3TestBean
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
testbean.name=p4TestBean

0 commit comments

Comments
 (0)