diff --git a/spring-boot/src/main/java/org/springframework/boot/context/listener/ConfigFileApplicationListener.java b/spring-boot/src/main/java/org/springframework/boot/context/listener/ConfigFileApplicationListener.java index db27bc6b20..1a128d4596 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/listener/ConfigFileApplicationListener.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/listener/ConfigFileApplicationListener.java @@ -182,7 +182,7 @@ public class ConfigFileApplicationListener implements private void load(ConfigurableEnvironment environment, ResourceLoader resourceLoader) { - List candidates = getCandidateLocations(resourceLoader); + List 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); } - environment.getPropertySources().addLast(source); + + PropertySource source = load(environment, resourceLoader, path, null); + if (source != null) { + if (first == null) { + first = source.getName(); + } + environment.getPropertySources().addLast(source); + } + } } @@ -232,12 +244,14 @@ public class ConfigFileApplicationListener implements } } - private List getCandidateLocations(ResourceLoader resourceLoader) { + private List getCandidateLocations(ConfigurableEnvironment environment, + ResourceLoader resourceLoader) { Set candidates = new LinkedHashSet(); 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); diff --git a/spring-boot/src/test/java/org/springframework/boot/context/listener/ConfigFileApplicationListenerTests.java b/spring-boot/src/test/java/org/springframework/boot/context/listener/ConfigFileApplicationListenerTests.java index 4c6361830b..92e0834c9d 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/listener/ConfigFileApplicationListenerTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/listener/ConfigFileApplicationListenerTests.java @@ -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 map = new HashMap(); - 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"));