From 393cfe505e20496dc69b54aa94ba944dcdb75f0f Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Mon, 23 Jan 2017 11:16:17 +0000 Subject: [PATCH] Allow spring.profiles.include to be used anywhere to add active profiles 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 --- .../config/ConfigFileApplicationListener.java | 7 +++++-- .../config/ConfigFileApplicationListenerTests.java | 12 ++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java b/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java index 85c6bb7b93..46dec38815 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java @@ -393,8 +393,11 @@ public class ConfigFileApplicationListener implements EnvironmentPostProcessor, } // Any pre-existing active profiles set via property sources (e.g. System // properties) take precedence over those added in config files. - Set activeProfiles = bindSpringProfiles( - this.environment.getPropertySources()).getActiveProfiles(); + SpringProfiles springProfiles = bindSpringProfiles( + this.environment.getPropertySources()); + Set activeProfiles = new LinkedHashSet( + springProfiles.getActiveProfiles()); + activeProfiles.addAll(springProfiles.getIncludeProfiles()); maybeActivateProfiles(activeProfiles); return activeProfiles; } diff --git a/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java b/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java index 4221a73229..3d67ce6934 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java @@ -837,6 +837,18 @@ public class ConfigFileApplicationListenerTests { 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 matchingPropertySource( final String sourceName) { return new Condition(