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 1e69e323614dc15772c2139950aebe90be384298..9577d3d551b2744022354d1db91c7bb4b7f96dc5 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 aaf2a82c18f7d7a487561398341e770590531908..45673cfd654d6de2d628ce4ec12cb8e47d280bae 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 = {})