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:
parent
e71fbb9f46
commit
a2b983a4e4
|
@ -128,7 +128,7 @@ class ConfigurationClassParser {
|
|||
|
||||
private final Map<String, ConfigurationClass> knownSuperclasses = new HashMap<String, ConfigurationClass>();
|
||||
|
||||
private final Set<String> propertySourceNames = new LinkedHashSet<String>();
|
||||
private final List<String> propertySourceNames = new ArrayList<String>();
|
||||
|
||||
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);
|
||||
|
|
|
@ -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")
|
||||
|
|
Loading…
Reference in New Issue