Skip to content

Commit

Permalink
Allow spring.profiles.include to be used anywhere to add active profiles
Browse files Browse the repository at this point in the history
Previously, spring.profiles.include was only considered when it was
used in a configuration file. It was ignored in any other property
source.

This commit updates ConfigFileApplicationListener so that
spring.profiles.include can be used in any property source to add to
the profiles that have been declared active via
spring.profiles.active.

Closes gh-7668
  • Loading branch information
wilkinsona committed Jan 23, 2017
1 parent 07d9c3f commit 393cfe5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -393,8 +393,11 @@ private Set<Profile> initializeActiveProfiles() {
}
// Any pre-existing active profiles set via property sources (e.g. System
// properties) take precedence over those added in config files.
Set<Profile> activeProfiles = bindSpringProfiles(
this.environment.getPropertySources()).getActiveProfiles();
SpringProfiles springProfiles = bindSpringProfiles(
this.environment.getPropertySources());
Set<Profile> activeProfiles = new LinkedHashSet<Profile>(
springProfiles.getActiveProfiles());
activeProfiles.addAll(springProfiles.getIncludeProfiles());
maybeActivateProfiles(activeProfiles);
return activeProfiles;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,18 @@ public void customDefaultProfileAndActiveFromFile() throws Exception {
assertThat(environment.acceptsProfiles("customdefault")).isTrue();
}

@Test
public void additionalProfilesCanBeIncludedFromAnyPropertySource() throws Exception {
SpringApplication application = new SpringApplication(Config.class);
application.setWebEnvironment(false);
this.context = application.run("--spring.profiles.active=myprofile",
"--spring.profiles.include=dev");
String property = this.context.getEnvironment().getProperty("my.property");
assertThat(property).isEqualTo("fromdevpropertiesfile");
assertThat(this.context.getEnvironment().containsProperty("customdefault"))
.isFalse();
}

private Condition<ConfigurableEnvironment> matchingPropertySource(
final String sourceName) {
return new Condition<ConfigurableEnvironment>(
Expand Down

0 comments on commit 393cfe5

Please sign in to comment.