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
master
Phillip Webb 10 years ago
parent e71fbb9f46
commit a2b983a4e4
  1. 5
      spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java
  2. 15
      spring-context/src/test/java/org/springframework/context/annotation/PropertySourceAnnotationTests.java

@ -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…
Cancel
Save