From e3d3d8cd9569ceb5db3dd00d8104a979c7dfe82d Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Mon, 21 Oct 2013 12:59:00 -0700 Subject: [PATCH] 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 --- .../annotation/ConfigurationClassParser.java | 4 ++-- .../PropertySourceAnnotationTests.java | 21 +++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) 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 1e69e32361..9577d3d551 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 @@ -320,8 +320,8 @@ class ConfigurationClassParser { } else { CompositePropertySource ps = new CompositePropertySource(name); - for (String location : locations) { - ps.addPropertySource(new ResourcePropertySource(location, classLoader)); + for (int i = locations.length - 1; i >= 0; i--) { + ps.addPropertySource(new ResourcePropertySource(locations[i], classLoader)); } this.propertySources.push(ps); } 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 aaf2a82c18..45673cfd65 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 @@ -130,6 +130,18 @@ public class PropertySourceAnnotationTests { 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) public void withEmptyResourceLocations() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); @@ -209,6 +221,15 @@ public class PropertySourceAnnotationTests { static class ConfigWithNameAndMultipleResourceLocations { } + @Configuration + @PropertySource( + value = { + "classpath:org/springframework/context/annotation/p1.properties", + "classpath:org/springframework/context/annotation/p2.properties" + }) + static class ConfigWithMultipleResourceLocations { + } + @Configuration @PropertySource(value = {}) -- GitLab