Commit 3a35c95e authored by Dave Syer's avatar Dave Syer

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
parent dac1b53f
...@@ -182,7 +182,7 @@ public class ConfigFileApplicationListener implements ...@@ -182,7 +182,7 @@ public class ConfigFileApplicationListener implements
private void load(ConfigurableEnvironment environment, ResourceLoader resourceLoader) { private void load(ConfigurableEnvironment environment, ResourceLoader resourceLoader) {
List<String> candidates = getCandidateLocations(resourceLoader); List<String> candidates = getCandidateLocations(environment, resourceLoader);
Collections.reverse(candidates); Collections.reverse(candidates);
PropertySource<?> removed = environment.getPropertySources().remove( PropertySource<?> removed = environment.getPropertySources().remove(
"defaultProperties"); "defaultProperties");
...@@ -190,12 +190,24 @@ public class ConfigFileApplicationListener implements ...@@ -190,12 +190,24 @@ public class ConfigFileApplicationListener implements
String first = null; String first = null;
// Initial load allows profiles to be activated // Initial load allows profiles to be activated
for (String candidate : candidates) { for (String candidate : candidates) {
PropertySource<?> source = load(environment, resourceLoader, candidate, null); for (String path : StringUtils.commaDelimitedListToStringArray(environment
if (source != null) { .resolvePlaceholders(candidate))) {
if (first == null) {
first = source.getName(); 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 ...@@ -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>(); Set<String> candidates = new LinkedHashSet<String>();
for (String searchLocation : this.searchLocations) { for (String searchLocation : this.searchLocations) {
for (String extension : new String[] { ".properties", ".yml" }) { for (String extension : new String[] { ".properties", ".yml" }) {
for (String name : StringUtils for (String name : StringUtils
.commaDelimitedListToStringArray(this.names)) { .commaDelimitedListToStringArray(environment
.resolvePlaceholders(this.names))) {
String location = searchLocation + name + extension; String location = searchLocation + name + extension;
candidates.add(location); candidates.add(location);
} }
...@@ -264,15 +278,6 @@ public class ConfigFileApplicationListener implements ...@@ -264,15 +278,6 @@ public class ConfigFileApplicationListener implements
private PropertySource<?> load(ConfigurableEnvironment environment, private PropertySource<?> load(ConfigurableEnvironment environment,
ResourceLoader resourceLoader, String location, String profile) { 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); String suffix = "." + StringUtils.getFilenameExtension(location);
Class<?> type = this.propertySourceAnnotations.configuration(location); Class<?> type = this.propertySourceAnnotations.configuration(location);
......
...@@ -17,9 +17,7 @@ ...@@ -17,9 +17,7 @@
package org.springframework.boot.context.listener; package org.springframework.boot.context.listener;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import org.junit.After; import org.junit.After;
import org.junit.Rule; import org.junit.Rule;
...@@ -35,7 +33,6 @@ import org.springframework.context.annotation.Configuration; ...@@ -35,7 +33,6 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile; import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.PropertySource; import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.SimpleCommandLinePropertySource; import org.springframework.core.env.SimpleCommandLinePropertySource;
import org.springframework.core.env.StandardEnvironment; import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
...@@ -81,6 +78,28 @@ public class ConfigFileApplicationListenerTests { ...@@ -81,6 +78,28 @@ public class ConfigFileApplicationListenerTests {
assertThat(property, equalTo("frompropertiesfile")); 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 @Test
public void randomValue() throws Exception { public void randomValue() throws Exception {
this.initializer.onApplicationEvent(this.event); this.initializer.onApplicationEvent(this.event);
...@@ -173,11 +192,9 @@ public class ConfigFileApplicationListenerTests { ...@@ -173,11 +192,9 @@ public class ConfigFileApplicationListenerTests {
@Test @Test
public void specificNameAndProfileFromExistingSource() throws Exception { public void specificNameAndProfileFromExistingSource() throws Exception {
Map<String, Object> map = new HashMap<String, Object>(); EnvironmentTestUtils.addEnvironment(this.environment,
map.put("spring.profiles.active", "specificprofile"); "spring.profiles.active=specificprofile",
map.put("spring.config.name", "specificfile"); "spring.config.name=specificfile");
MapPropertySource source = new MapPropertySource("map", map);
this.environment.getPropertySources().addFirst(source);
this.initializer.onApplicationEvent(this.event); this.initializer.onApplicationEvent(this.event);
String property = this.environment.getProperty("my.property"); String property = this.environment.getProperty("my.property");
assertThat(property, equalTo("fromspecificpropertiesfile")); assertThat(property, equalTo("fromspecificpropertiesfile"));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment