Add config file property sources after existing

Change ConfigFileApplicationContextInitializer to add config file
property sources after existing sources. This allows environment
variables and system properties to override file properties.

Issue: #55739594
This commit is contained in:
Phillip Webb
2013-08-23 12:07:35 -07:00
parent 21766b8183
commit 37d136dcb6
2 changed files with 18 additions and 9 deletions

View File

@@ -17,6 +17,7 @@
package org.springframework.boot.context.initializer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -39,7 +40,6 @@ import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.core.env.CommandLinePropertySource;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
@@ -127,6 +127,7 @@ public class ConfigFileApplicationContextInitializer implements
private void load(ConfigurableEnvironment environment, ResourceLoader resourceLoader) {
List<String> candidates = getCandidateLocations();
Collections.reverse(candidates);
// Initial load allows profiles to be activated
for (String candidate : candidates) {
@@ -186,14 +187,7 @@ public class ConfigFileApplicationContextInitializer implements
}
}
MutablePropertySources propertySources = environment.getPropertySources();
if (propertySources.contains(COMMAND_LINE_PROPERTY_SOURCE_NAME)) {
propertySources.addAfter(COMMAND_LINE_PROPERTY_SOURCE_NAME, propertySource);
}
else {
propertySources.addFirst(propertySource);
}
environment.getPropertySources().addLast(propertySource);
}
private PropertySource<?> getPropertySource(Resource resource,

View File

@@ -19,6 +19,7 @@ package org.springframework.boot.context.initializer;
import java.util.HashMap;
import java.util.Map;
import org.junit.After;
import org.junit.Test;
import org.springframework.context.support.StaticApplicationContext;
import org.springframework.core.env.MapPropertySource;
@@ -40,6 +41,11 @@ public class ConfigFileApplicationContextInitializerTests {
private ConfigFileApplicationContextInitializer initializer = new ConfigFileApplicationContextInitializer();
@After
public void cleanup() {
System.clearProperty("my.property");
}
@Test
public void loadPropertiesFile() throws Exception {
this.initializer.setNames("testproperties");
@@ -81,6 +87,15 @@ public class ConfigFileApplicationContextInitializerTests {
assertThat(property, equalTo("fromcommandline"));
}
@Test
public void systemPropertyWins() throws Exception {
System.setProperty("my.property", "fromsystem");
this.initializer.setNames("testproperties");
this.initializer.initialize(this.context);
String property = this.context.getEnvironment().getProperty("my.property");
assertThat(property, equalTo("fromsystem"));
}
@Test
public void loadPropertiesThenProfileProperties() throws Exception {
this.initializer.setNames("enableprofile");