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
This commit is contained in:
Phillip Webb
2014-09-19 00:07:30 -07:00
parent e71fbb9f46
commit a2b983a4e4
2 changed files with 17 additions and 3 deletions

View File

@@ -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")