Commit dc8ba2c5 authored by Dave Syer's avatar Dave Syer

Load configuration from default profiles if no others active

Before this change if no profile is active yaml documents with
spring.profiles=default were loaded, but they are also loaded
if there *is* an active profile which is more of a problem. In
addition if the user chanes the default profile in the
Environmemt Spring Boot ignore that value ("default" is a magic
String).

After this change:

* If no profile is explicitly active, the default profiles from
the Environment are used explicitly
* The default profiles cause properties to be loaded just like
other profiles, so from YAML documents with spring.profiles and
from files in application-default.properties for instance
* The default profiles are not active when any other profile is
* Properties defined in "top-level" YAML documents with no
specific spring.profiles still act as defaults for *all* profiles

Fixes gh-1219, fixes gh-2623
parent dfbabefc
...@@ -322,6 +322,14 @@ public class ConfigFileApplicationListener implements ...@@ -322,6 +322,14 @@ public class ConfigFileApplicationListener implements
this.profiles.addAll(list); 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 // 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 // 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). // override any settings in the defaults when the list is reversed later).
...@@ -376,7 +384,7 @@ public class ConfigFileApplicationListener implements ...@@ -376,7 +384,7 @@ public class ConfigFileApplicationListener implements
String profile) throws IOException { String profile) throws IOException {
Resource resource = this.resourceLoader.getResource(location); Resource resource = this.resourceLoader.getResource(location);
PropertySource<?> propertySource = null; PropertySource<?> propertySource = null;
if (resource != null) { if (resource != null && resource.exists()) {
String name = "applicationConfig: [" + location + "]"; String name = "applicationConfig: [" + location + "]";
String group = "applicationConfig: [" + identifier + "]"; String group = "applicationConfig: [" + identifier + "]";
propertySource = this.propertiesLoader.load(resource, group, name, propertySource = this.propertiesLoader.load(resource, group, name,
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
package org.springframework.boot.yaml; package org.springframework.boot.yaml;
import java.util.Collections;
import java.util.Properties; import java.util.Properties;
import java.util.Set; import java.util.Set;
...@@ -49,6 +50,9 @@ public class ArrayDocumentMatcher implements DocumentMatcher { ...@@ -49,6 +50,9 @@ public class ArrayDocumentMatcher implements DocumentMatcher {
} }
Set<String> values = StringUtils.commaDelimitedListToSet(properties Set<String> values = StringUtils.commaDelimitedListToSet(properties
.getProperty(this.key)); .getProperty(this.key));
if (values.isEmpty()) {
values = Collections.singleton("");
}
for (String pattern : this.patterns) { for (String pattern : this.patterns) {
for (String value : values) { for (String value : values) {
if (value.matches(pattern)) { if (value.matches(pattern)) {
......
...@@ -34,7 +34,7 @@ import org.springframework.core.env.Environment; ...@@ -34,7 +34,7 @@ import org.springframework.core.env.Environment;
*/ */
public class SpringProfileDocumentMatcher implements DocumentMatcher { 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]; private String[] activeProfiles = new String[0];
......
...@@ -123,6 +123,15 @@ public class ConfigFileApplicationListenerTests { ...@@ -123,6 +123,15 @@ public class ConfigFileApplicationListenerTests {
assertThat(property, equalTo("frompropertiesfile")); 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 @Test
public void loadTwoPropertiesFile() throws Exception { public void loadTwoPropertiesFile() throws Exception {
EnvironmentTestUtils.addEnvironment(this.environment, "spring.config.location:" EnvironmentTestUtils.addEnvironment(this.environment, "spring.config.location:"
...@@ -243,6 +252,33 @@ public class ConfigFileApplicationListenerTests { ...@@ -243,6 +252,33 @@ public class ConfigFileApplicationListenerTests {
assertThat(this.environment.getProperty("my.array"), nullValue(String.class)); 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 @Test
public void commandLineWins() throws Exception { public void commandLineWins() throws Exception {
this.environment.getPropertySources().addFirst( this.environment.getPropertySources().addFirst(
......
my.property=fromdefaultpropertiesfile
the.property=fromdefaultpropertiesfile
---
my:
property: fromyamlfile
other: notempty
---
spring:
profiles: thedefault
my:
property: fromdefaultprofile
---
spring:
profiles: other
my:
property: fromotherprofile
\ No newline at end of file
---
my:
property: fromyamlfile
other: notempty
---
spring:
profiles:
my:
property: fromemptyprofile
---
spring:
profiles: other
my:
property: fromotherprofile
\ No newline at end of file
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