From a2b983a4e400123563b3923c2cbcfd23b568d44d Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Fri, 19 Sep 2014 00:07:30 -0700 Subject: [PATCH] Further refine property source ordering Refine property source ordering so that sources already contained in the environment remain before those added by @PropertySource annotations. Issue: SPR-12198 --- .../annotation/ConfigurationClassParser.java | 5 +++-- .../annotation/PropertySourceAnnotationTests.java | 15 ++++++++++++++- 2 files changed, 17 insertions(+), 3 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 d99b4ad266..2dd2a20df2 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 @@ -128,7 +128,7 @@ class ConfigurationClassParser { private final Map knownSuperclasses = new HashMap(); - private final Set propertySourceNames = new LinkedHashSet(); + private final List propertySourceNames = new ArrayList(); private final ImportStack importStack = new ImportStack(); @@ -375,7 +375,8 @@ class ConfigurationClassParser { propertySources.addLast(propertySource); } else { - propertySources.addFirst(propertySource); + String firstProcessed = this.propertySourceNames.get(this.propertySourceNames.size()-1); + propertySources.addBefore(firstProcessed, 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 b699d48268..cc4acf6dc9 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 @@ -17,16 +17,18 @@ package org.springframework.context.annotation; import java.io.FileNotFoundException; +import java.util.Collections; import java.util.Iterator; + import javax.inject.Inject; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; - import org.springframework.beans.factory.BeanDefinitionStoreException; import org.springframework.beans.factory.FactoryBean; import org.springframework.core.env.Environment; +import org.springframework.core.env.MapPropertySource; import org.springframework.core.env.MutablePropertySources; import org.springframework.tests.sample.beans.TestBean; @@ -235,6 +237,17 @@ public class PropertySourceAnnotationTests { assertThat(ctxWithoutName.getEnvironment().getProperty("testbean.name"), equalTo("p4TestBean")); } + @Test + public void orderingDoesntReplaceExisting() throws Exception { + // SPR-12198: mySource should 'win' as it was registered manually + AnnotationConfigApplicationContext ctxWithoutName = new AnnotationConfigApplicationContext(); + MapPropertySource mySource = new MapPropertySource("mine", Collections.singletonMap("testbean.name", "myTestBean")); + ctxWithoutName.getEnvironment().getPropertySources().addLast(mySource); + ctxWithoutName.register(ConfigWithFourResourceLocations.class); + ctxWithoutName.refresh(); + assertThat(ctxWithoutName.getEnvironment().getProperty("testbean.name"), equalTo("myTestBean")); + + } @Configuration @PropertySource(value="classpath:${unresolvable}/p1.properties")