Extend semantics of ConfigFileApplicationListener environment a bit

spring.config.[name,location] can both be a comma-separated list now. Highest
priority is last, like a hash overriding its keys (as per other conventions
in the listener).

Fixes gh-261
pull/272/head
Dave Syer 11 years ago
parent dac1b53fda
commit 3a35c95e63

@ -182,7 +182,7 @@ public class ConfigFileApplicationListener implements
private void load(ConfigurableEnvironment environment, ResourceLoader resourceLoader) {
List<String> candidates = getCandidateLocations(resourceLoader);
List<String> candidates = getCandidateLocations(environment, resourceLoader);
Collections.reverse(candidates);
PropertySource<?> removed = environment.getPropertySources().remove(
"defaultProperties");
@ -190,12 +190,24 @@ public class ConfigFileApplicationListener implements
String first = null;
// Initial load allows profiles to be activated
for (String candidate : candidates) {
PropertySource<?> source = load(environment, resourceLoader, candidate, null);
if (source != null) {
if (first == null) {
first = source.getName();
for (String path : StringUtils.commaDelimitedListToStringArray(environment
.resolvePlaceholders(candidate))) {
if (LOCATION_VARIABLE.equals(candidate) && !path.contains("$")) {
if (!path.contains(":")) {
path = "file:" + path;
}
path = StringUtils.cleanPath(path);
}
PropertySource<?> source = load(environment, resourceLoader, path, null);
if (source != null) {
if (first == null) {
first = source.getName();
}
environment.getPropertySources().addLast(source);
}
environment.getPropertySources().addLast(source);
}
}
@ -232,12 +244,14 @@ public class ConfigFileApplicationListener implements
}
}
private List<String> getCandidateLocations(ResourceLoader resourceLoader) {
private List<String> getCandidateLocations(ConfigurableEnvironment environment,
ResourceLoader resourceLoader) {
Set<String> candidates = new LinkedHashSet<String>();
for (String searchLocation : this.searchLocations) {
for (String extension : new String[] { ".properties", ".yml" }) {
for (String name : StringUtils
.commaDelimitedListToStringArray(this.names)) {
.commaDelimitedListToStringArray(environment
.resolvePlaceholders(this.names))) {
String location = searchLocation + name + extension;
candidates.add(location);
}
@ -264,15 +278,6 @@ public class ConfigFileApplicationListener implements
private PropertySource<?> load(ConfigurableEnvironment environment,
ResourceLoader resourceLoader, String location, String profile) {
String path = environment.resolvePlaceholders(location);
if (LOCATION_VARIABLE.equals(location) && !path.contains("$")) {
if (!path.contains(":")) {
path = "file:" + path;
}
path = StringUtils.cleanPath(path);
}
location = path;
String suffix = "." + StringUtils.getFilenameExtension(location);
Class<?> type = this.propertySourceAnnotations.configuration(location);

@ -17,9 +17,7 @@
package org.springframework.boot.context.listener;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.After;
import org.junit.Rule;
@ -35,7 +33,6 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.SimpleCommandLinePropertySource;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.io.Resource;
@ -81,6 +78,28 @@ public class ConfigFileApplicationListenerTests {
assertThat(property, equalTo("frompropertiesfile"));
}
@Test
public void loadTwoPropertiesFile() throws Exception {
EnvironmentTestUtils
.addEnvironment(
this.environment,
"spring.config.location:classpath:testproperties.properties,classpath:application.properties");
this.initializer.onApplicationEvent(this.event);
String property = this.environment.getProperty("my.property");
assertThat(property, equalTo("frompropertiesfile"));
}
@Test
public void loadTwoOfThreePropertiesFile() throws Exception {
EnvironmentTestUtils
.addEnvironment(
this.environment,
"spring.config.location:classpath:testproperties.properties,classpath:application.properties,classpath:nonexistent.properties");
this.initializer.onApplicationEvent(this.event);
String property = this.environment.getProperty("my.property");
assertThat(property, equalTo("frompropertiesfile"));
}
@Test
public void randomValue() throws Exception {
this.initializer.onApplicationEvent(this.event);
@ -173,11 +192,9 @@ public class ConfigFileApplicationListenerTests {
@Test
public void specificNameAndProfileFromExistingSource() throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
map.put("spring.profiles.active", "specificprofile");
map.put("spring.config.name", "specificfile");
MapPropertySource source = new MapPropertySource("map", map);
this.environment.getPropertySources().addFirst(source);
EnvironmentTestUtils.addEnvironment(this.environment,
"spring.profiles.active=specificprofile",
"spring.config.name=specificfile");
this.initializer.onApplicationEvent(this.event);
String property = this.environment.getProperty("my.property");
assertThat(property, equalTo("fromspecificpropertiesfile"));

Loading…
Cancel
Save