From 90b93ffe3f74b651c2bf3a8bbd1426674eb8544e Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Thu, 18 Sep 2014 23:48:35 +0200 Subject: [PATCH] Restored property source ordering (fixing regression from 4.0.x) Issue: SPR-12198 --- .../annotation/ConfigurationClassParser.java | 3 +-- .../PropertySourceAnnotationTests.java | 25 ++++++++++++++++--- .../context/annotation/p3.properties | 1 + .../context/annotation/p4.properties | 1 + 4 files changed, 25 insertions(+), 5 deletions(-) create mode 100644 spring-context/src/test/java/org/springframework/context/annotation/p3.properties create mode 100644 spring-context/src/test/java/org/springframework/context/annotation/p4.properties diff --git a/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java b/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java index 8458342e688..d99b4ad266b 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java @@ -375,8 +375,7 @@ class ConfigurationClassParser { propertySources.addLast(propertySource); } else { - String firstProcessed = this.propertySourceNames.iterator().next(); - propertySources.addBefore(firstProcessed, propertySource); + propertySources.addFirst(propertySource); } } this.propertySourceNames.add(name); diff --git a/spring-context/src/test/java/org/springframework/context/annotation/PropertySourceAnnotationTests.java b/spring-context/src/test/java/org/springframework/context/annotation/PropertySourceAnnotationTests.java index 19f15c88f19..becd3ec8ae9 100644 --- a/spring-context/src/test/java/org/springframework/context/annotation/PropertySourceAnnotationTests.java +++ b/spring-context/src/test/java/org/springframework/context/annotation/PropertySourceAnnotationTests.java @@ -229,6 +229,13 @@ public class PropertySourceAnnotationTests { assertThat(ctx.getEnvironment().getProperty("testbean.name"), equalTo("p2TestBean")); } + @Test + public void orderingWithAndWithoutNameAndFourResourceLocations() { + // SPR-12198: p4 should 'win' as it was registered last + AnnotationConfigApplicationContext ctxWithoutName = new AnnotationConfigApplicationContext(ConfigWithFourResourceLocations.class); + assertThat(ctxWithoutName.getEnvironment().getProperty("testbean.name"), equalTo("p4TestBean")); + } + @Configuration @PropertySource(value="classpath:${unresolvable}/p1.properties") @@ -367,8 +374,8 @@ public class PropertySourceAnnotationTests { @Configuration @PropertySources({ - @PropertySource(name = "psName", value = "classpath:org/springframework/context/annotation/p1.properties"), - @PropertySource(name = "psName", value = "classpath:org/springframework/context/annotation/p2.properties"), + @PropertySource(name = "psName", value = "classpath:org/springframework/context/annotation/p1.properties"), + @PropertySource(name = "psName", value = "classpath:org/springframework/context/annotation/p2.properties"), }) static class ConfigWithNamedPropertySources { } @@ -401,7 +408,7 @@ public class PropertySourceAnnotationTests { } - @Import({ ConfigImportedWithSameSourceImportedInDifferentOrder.class }) + @Import(ConfigImportedWithSameSourceImportedInDifferentOrder.class) @PropertySources({ @PropertySource("classpath:org/springframework/context/annotation/p1.properties"), @PropertySource("classpath:org/springframework/context/annotation/p2.properties") @@ -420,4 +427,16 @@ public class PropertySourceAnnotationTests { public static class ConfigImportedWithSameSourceImportedInDifferentOrder { } + + @Configuration + @PropertySource( + value = { + "classpath:org/springframework/context/annotation/p1.properties", + "classpath:org/springframework/context/annotation/p2.properties", + "classpath:org/springframework/context/annotation/p3.properties", + "classpath:org/springframework/context/annotation/p4.properties" + }) + static class ConfigWithFourResourceLocations { + } + } diff --git a/spring-context/src/test/java/org/springframework/context/annotation/p3.properties b/spring-context/src/test/java/org/springframework/context/annotation/p3.properties new file mode 100644 index 00000000000..5165c44d452 --- /dev/null +++ b/spring-context/src/test/java/org/springframework/context/annotation/p3.properties @@ -0,0 +1 @@ +testbean.name=p3TestBean diff --git a/spring-context/src/test/java/org/springframework/context/annotation/p4.properties b/spring-context/src/test/java/org/springframework/context/annotation/p4.properties new file mode 100644 index 00000000000..a9fbccd9826 --- /dev/null +++ b/spring-context/src/test/java/org/springframework/context/annotation/p4.properties @@ -0,0 +1 @@ +testbean.name=p4TestBean