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
This commit is contained in:
Phillip Webb
2016-09-14 16:31:31 -07:00
parent dcfe2673fd
commit 0606428609
4 changed files with 15 additions and 3 deletions

View File

@@ -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,
}
}
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>();

View File

@@ -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(

View File

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

View File

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