diff --git a/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java b/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java index c655c1015ea..d2f987d30b5 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java @@ -322,6 +322,14 @@ public class ConfigFileApplicationListener implements this.profiles.addAll(list); } + if (this.profiles.isEmpty()) { + for (String defaultProfile : this.environment.getDefaultProfiles()) { + if (!this.profiles.contains(defaultProfile)) { + this.profiles.add(defaultProfile); + } + } + } + // The default profile for these purposes is represented as null. We add it // last so that it is first out of the queue (active profiles will then // override any settings in the defaults when the list is reversed later). @@ -376,7 +384,7 @@ public class ConfigFileApplicationListener implements String profile) throws IOException { Resource resource = this.resourceLoader.getResource(location); PropertySource propertySource = null; - if (resource != null) { + if (resource != null && resource.exists()) { String name = "applicationConfig: [" + location + "]"; String group = "applicationConfig: [" + identifier + "]"; propertySource = this.propertiesLoader.load(resource, group, name, diff --git a/spring-boot/src/main/java/org/springframework/boot/yaml/ArrayDocumentMatcher.java b/spring-boot/src/main/java/org/springframework/boot/yaml/ArrayDocumentMatcher.java index 295f9be0d49..9198647d8a2 100644 --- a/spring-boot/src/main/java/org/springframework/boot/yaml/ArrayDocumentMatcher.java +++ b/spring-boot/src/main/java/org/springframework/boot/yaml/ArrayDocumentMatcher.java @@ -16,6 +16,7 @@ package org.springframework.boot.yaml; +import java.util.Collections; import java.util.Properties; import java.util.Set; @@ -49,6 +50,9 @@ public class ArrayDocumentMatcher implements DocumentMatcher { } Set values = StringUtils.commaDelimitedListToSet(properties .getProperty(this.key)); + if (values.isEmpty()) { + values = Collections.singleton(""); + } for (String pattern : this.patterns) { for (String value : values) { if (value.matches(pattern)) { diff --git a/spring-boot/src/main/java/org/springframework/boot/yaml/SpringProfileDocumentMatcher.java b/spring-boot/src/main/java/org/springframework/boot/yaml/SpringProfileDocumentMatcher.java index b994c6a007c..e7caf424a22 100644 --- a/spring-boot/src/main/java/org/springframework/boot/yaml/SpringProfileDocumentMatcher.java +++ b/spring-boot/src/main/java/org/springframework/boot/yaml/SpringProfileDocumentMatcher.java @@ -34,7 +34,7 @@ import org.springframework.core.env.Environment; */ public class SpringProfileDocumentMatcher implements DocumentMatcher { - private static final String[] DEFAULT_PROFILES = new String[] { "default" }; + private static final String[] DEFAULT_PROFILES = new String[] { "^\\s*$" }; private String[] activeProfiles = new String[0]; diff --git a/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java b/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java index f23836071e6..cac874fe494 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java @@ -123,6 +123,15 @@ public class ConfigFileApplicationListenerTests { assertThat(property, equalTo("frompropertiesfile")); } + @Test + public void loadDefaultPropertiesFile() throws Exception { + this.environment.setDefaultProfiles("thedefault"); + this.initializer.setSearchNames("testprofiles"); + this.initializer.onApplicationEvent(this.event); + String property = this.environment.getProperty("the.property"); + assertThat(property, equalTo("fromdefaultpropertiesfile")); + } + @Test public void loadTwoPropertiesFile() throws Exception { EnvironmentTestUtils.addEnvironment(this.environment, "spring.config.location:" @@ -243,6 +252,33 @@ public class ConfigFileApplicationListenerTests { assertThat(this.environment.getProperty("my.array"), nullValue(String.class)); } + @Test + public void loadProfileEmptySameAsNotSpecified() throws Exception { + this.initializer.setSearchNames("testprofilesempty"); + this.initializer.onApplicationEvent(this.event); + String property = this.environment.getProperty("my.property"); + assertThat(property, equalTo("fromemptyprofile")); + } + + @Test + public void loadDefaultYamlDocument() throws Exception { + this.environment.setDefaultProfiles("thedefault"); + this.initializer.setSearchNames("testprofilesdocument"); + this.initializer.onApplicationEvent(this.event); + String property = this.environment.getProperty("my.property"); + assertThat(property, equalTo("fromdefaultprofile")); + } + + @Test + public void loadDefaultYamlDocumentNotActivated() throws Exception { + this.environment.setDefaultProfiles("thedefault"); + this.environment.setActiveProfiles("other"); + this.initializer.setSearchNames("testprofilesdocument"); + this.initializer.onApplicationEvent(this.event); + String property = this.environment.getProperty("my.property"); + assertThat(property, equalTo("fromotherprofile")); + } + @Test public void commandLineWins() throws Exception { this.environment.getPropertySources().addFirst( diff --git a/spring-boot/src/test/resources/testprofiles-thedefault.properties b/spring-boot/src/test/resources/testprofiles-thedefault.properties new file mode 100644 index 00000000000..bc81eb60a66 --- /dev/null +++ b/spring-boot/src/test/resources/testprofiles-thedefault.properties @@ -0,0 +1,2 @@ +my.property=fromdefaultpropertiesfile +the.property=fromdefaultpropertiesfile diff --git a/spring-boot/src/test/resources/testprofilesdocument.yml b/spring-boot/src/test/resources/testprofilesdocument.yml new file mode 100644 index 00000000000..0093d6274c9 --- /dev/null +++ b/spring-boot/src/test/resources/testprofilesdocument.yml @@ -0,0 +1,14 @@ +--- +my: + property: fromyamlfile + other: notempty +--- +spring: + profiles: thedefault +my: + property: fromdefaultprofile +--- +spring: + profiles: other +my: + property: fromotherprofile \ No newline at end of file diff --git a/spring-boot/src/test/resources/testprofilesempty.yml b/spring-boot/src/test/resources/testprofilesempty.yml new file mode 100644 index 00000000000..4509623151f --- /dev/null +++ b/spring-boot/src/test/resources/testprofilesempty.yml @@ -0,0 +1,14 @@ +--- +my: + property: fromyamlfile + other: notempty +--- +spring: + profiles: +my: + property: fromemptyprofile +--- +spring: + profiles: other +my: + property: fromotherprofile \ No newline at end of file