Browse Source

Allow default profile to also be set in properties

Update `ConfigFileApplicationListener` so that active profiles set in
properties files that overlap with `spring.profiles.default` can still
be set.

Prior to this commit if `spring.profiles.active` happened to specify
a profile name that was also in `spring.profiles.default` it would
not get applied.

Fixes gh-6833
pull/6891/merge
Phillip Webb 9 years ago
parent
commit
0606428609
  1. 11
      spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java
  2. 4
      spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java
  3. 1
      spring-boot/src/test/resources/customprofile-customdefault.properties
  4. 2
      spring-boot/src/test/resources/customprofile.properties

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

@ -553,7 +553,7 @@ public class ConfigFileApplicationListener implements EnvironmentPostProcessor, @@ -553,7 +553,7 @@ public class ConfigFileApplicationListener implements EnvironmentPostProcessor,
private void addProfiles(Set<Profile> profiles) {
for (Profile profile : profiles) {
this.profiles.add(profile);
if (!this.environment.acceptsProfiles(profile.getName())) {
if (!environmentHasActiveProfile(profile.getName())) {
// If it's already accepted we assume the order was set
// intentionally
prependProfile(this.environment, profile);
@ -561,6 +561,15 @@ public class ConfigFileApplicationListener implements EnvironmentPostProcessor, @@ -561,6 +561,15 @@ public class ConfigFileApplicationListener implements EnvironmentPostProcessor,
}
}
private boolean environmentHasActiveProfile(String profile) {
for (String activeProfile : this.environment.getActiveProfiles()) {
if (activeProfile.equals(profile)) {
return true;
}
}
return false;
}
private void prependProfile(ConfigurableEnvironment environment,
Profile profile) {
Set<String> profiles = new LinkedHashSet<String>();

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

@ -824,7 +824,9 @@ public class ConfigFileApplicationListenerTests { @@ -824,7 +824,9 @@ public class ConfigFileApplicationListenerTests {
ConfigurableEnvironment environment = this.context.getEnvironment();
assertThat(environment.containsProperty("customprofile")).isTrue();
assertThat(environment.containsProperty("customprofile-specific")).isTrue();
assertThat(environment.containsProperty("customprofile-customdefault")).isFalse();
assertThat(environment.containsProperty("customprofile-customdefault")).isTrue();
assertThat(this.context.getEnvironment().acceptsProfiles("customdefault"))
.isTrue();
}
private Condition<ConfigurableEnvironment> matchingPropertySource(

1
spring-boot/src/test/resources/customprofile-customdefault.properties

@ -1 +1,2 @@ @@ -1 +1,2 @@
spring.profiles.include=specific
customprofile-customdefault=true

2
spring-boot/src/test/resources/customprofile.properties

@ -1,2 +1,2 @@ @@ -1,2 +1,2 @@
spring.profiles.active=specific
spring.profiles.active=customdefault
customprofile=true

Loading…
Cancel
Save