Skip to content

Commit dc8ba2c

Browse files
author
Dave Syer
committed
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 spring-projectsgh-1219, fixes spring-projectsgh-2623
1 parent dfbabef commit dc8ba2c

File tree

7 files changed

+80
-2
lines changed

7 files changed

+80
-2
lines changed

spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,14 @@ public void load() throws IOException {
322322
this.profiles.addAll(list);
323323
}
324324

325+
if (this.profiles.isEmpty()) {
326+
for (String defaultProfile : this.environment.getDefaultProfiles()) {
327+
if (!this.profiles.contains(defaultProfile)) {
328+
this.profiles.add(defaultProfile);
329+
}
330+
}
331+
}
332+
325333
// The default profile for these purposes is represented as null. We add it
326334
// last so that it is first out of the queue (active profiles will then
327335
// override any settings in the defaults when the list is reversed later).
@@ -376,7 +384,7 @@ private PropertySource<?> loadIntoGroup(String identifier, String location,
376384
String profile) throws IOException {
377385
Resource resource = this.resourceLoader.getResource(location);
378386
PropertySource<?> propertySource = null;
379-
if (resource != null) {
387+
if (resource != null && resource.exists()) {
380388
String name = "applicationConfig: [" + location + "]";
381389
String group = "applicationConfig: [" + identifier + "]";
382390
propertySource = this.propertiesLoader.load(resource, group, name,

spring-boot/src/main/java/org/springframework/boot/yaml/ArrayDocumentMatcher.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.boot.yaml;
1818

19+
import java.util.Collections;
1920
import java.util.Properties;
2021
import java.util.Set;
2122

@@ -49,6 +50,9 @@ public MatchStatus matches(Properties properties) {
4950
}
5051
Set<String> values = StringUtils.commaDelimitedListToSet(properties
5152
.getProperty(this.key));
53+
if (values.isEmpty()) {
54+
values = Collections.singleton("");
55+
}
5256
for (String pattern : this.patterns) {
5357
for (String value : values) {
5458
if (value.matches(pattern)) {

spring-boot/src/main/java/org/springframework/boot/yaml/SpringProfileDocumentMatcher.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
*/
3535
public class SpringProfileDocumentMatcher implements DocumentMatcher {
3636

37-
private static final String[] DEFAULT_PROFILES = new String[] { "default" };
37+
private static final String[] DEFAULT_PROFILES = new String[] { "^\\s*$" };
3838

3939
private String[] activeProfiles = new String[0];
4040

spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,15 @@ public void loadPropertiesFile() throws Exception {
123123
assertThat(property, equalTo("frompropertiesfile"));
124124
}
125125

126+
@Test
127+
public void loadDefaultPropertiesFile() throws Exception {
128+
this.environment.setDefaultProfiles("thedefault");
129+
this.initializer.setSearchNames("testprofiles");
130+
this.initializer.onApplicationEvent(this.event);
131+
String property = this.environment.getProperty("the.property");
132+
assertThat(property, equalTo("fromdefaultpropertiesfile"));
133+
}
134+
126135
@Test
127136
public void loadTwoPropertiesFile() throws Exception {
128137
EnvironmentTestUtils.addEnvironment(this.environment, "spring.config.location:"
@@ -243,6 +252,33 @@ public void loadYamlFile() throws Exception {
243252
assertThat(this.environment.getProperty("my.array"), nullValue(String.class));
244253
}
245254

255+
@Test
256+
public void loadProfileEmptySameAsNotSpecified() throws Exception {
257+
this.initializer.setSearchNames("testprofilesempty");
258+
this.initializer.onApplicationEvent(this.event);
259+
String property = this.environment.getProperty("my.property");
260+
assertThat(property, equalTo("fromemptyprofile"));
261+
}
262+
263+
@Test
264+
public void loadDefaultYamlDocument() throws Exception {
265+
this.environment.setDefaultProfiles("thedefault");
266+
this.initializer.setSearchNames("testprofilesdocument");
267+
this.initializer.onApplicationEvent(this.event);
268+
String property = this.environment.getProperty("my.property");
269+
assertThat(property, equalTo("fromdefaultprofile"));
270+
}
271+
272+
@Test
273+
public void loadDefaultYamlDocumentNotActivated() throws Exception {
274+
this.environment.setDefaultProfiles("thedefault");
275+
this.environment.setActiveProfiles("other");
276+
this.initializer.setSearchNames("testprofilesdocument");
277+
this.initializer.onApplicationEvent(this.event);
278+
String property = this.environment.getProperty("my.property");
279+
assertThat(property, equalTo("fromotherprofile"));
280+
}
281+
246282
@Test
247283
public void commandLineWins() throws Exception {
248284
this.environment.getPropertySources().addFirst(
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
my.property=fromdefaultpropertiesfile
2+
the.property=fromdefaultpropertiesfile
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
my:
3+
property: fromyamlfile
4+
other: notempty
5+
---
6+
spring:
7+
profiles: thedefault
8+
my:
9+
property: fromdefaultprofile
10+
---
11+
spring:
12+
profiles: other
13+
my:
14+
property: fromotherprofile
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
my:
3+
property: fromyamlfile
4+
other: notempty
5+
---
6+
spring:
7+
profiles:
8+
my:
9+
property: fromemptyprofile
10+
---
11+
spring:
12+
profiles: other
13+
my:
14+
property: fromotherprofile

0 commit comments

Comments
 (0)