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 8 years ago
parent dcfe2673fd
commit 0606428609

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

@ -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 +1,2 @@
spring.profiles.include=specific
customprofile-customdefault=true

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

Loading…
Cancel
Save