Commit 06064286 authored by Phillip Webb's avatar Phillip Webb

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
parent dcfe2673
...@@ -553,7 +553,7 @@ public class ConfigFileApplicationListener implements EnvironmentPostProcessor, ...@@ -553,7 +553,7 @@ public class ConfigFileApplicationListener implements EnvironmentPostProcessor,
private void addProfiles(Set<Profile> profiles) { private void addProfiles(Set<Profile> profiles) {
for (Profile profile : profiles) { for (Profile profile : profiles) {
this.profiles.add(profile); 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 // If it's already accepted we assume the order was set
// intentionally // intentionally
prependProfile(this.environment, profile); 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, private void prependProfile(ConfigurableEnvironment environment,
Profile profile) { Profile profile) {
Set<String> profiles = new LinkedHashSet<String>(); Set<String> profiles = new LinkedHashSet<String>();
......
...@@ -824,7 +824,9 @@ public class ConfigFileApplicationListenerTests { ...@@ -824,7 +824,9 @@ public class ConfigFileApplicationListenerTests {
ConfigurableEnvironment environment = this.context.getEnvironment(); ConfigurableEnvironment environment = this.context.getEnvironment();
assertThat(environment.containsProperty("customprofile")).isTrue(); assertThat(environment.containsProperty("customprofile")).isTrue();
assertThat(environment.containsProperty("customprofile-specific")).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( private Condition<ConfigurableEnvironment> matchingPropertySource(
......
spring.profiles.include=specific
customprofile-customdefault=true customprofile-customdefault=true
spring.profiles.active=specific spring.profiles.active=customdefault
customprofile=true customprofile=true
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