From 7dd8e8c4b8e9521aac55caef0cf94a14cea5c38e Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Thu, 9 Feb 2017 11:40:48 +0000 Subject: [PATCH] Honour spring.profiles.include even when .active has not been set 393cfe50 expanded the scope of spring.profiles.include so that it could be used in any property source, and not just in a configuration file. However, it did so in such a way that it would only take effect when used outside of a configuration file if spring.profiles.active was also set. This commit updates ConfigFileApplicationListener so that included profiles are activated when spring.profiles.active has not be set. Closes gh-8244 --- .../context/config/ConfigFileApplicationListener.java | 3 ++- .../config/ConfigFileApplicationListenerTests.java | 9 +++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) 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 14652a93de..a03c7dbcbf 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 @@ -388,7 +388,8 @@ public class ConfigFileApplicationListener implements EnvironmentPostProcessor, } private Set initializeActiveProfiles() { - if (!this.environment.containsProperty(ACTIVE_PROFILES_PROPERTY)) { + if (!this.environment.containsProperty(ACTIVE_PROFILES_PROPERTY) + && !this.environment.containsProperty(INCLUDE_PROFILES_PROPERTY)) { return Collections.emptySet(); } // Any pre-existing active profiles set via property sources (e.g. System 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 db6dd3977e..27a439cb5b 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 @@ -851,6 +851,15 @@ public class ConfigFileApplicationListenerTests { .isFalse(); } + @Test + public void profileCanBeIncludedWithoutAnyBeingActive() throws Exception { + SpringApplication application = new SpringApplication(Config.class); + application.setWebEnvironment(false); + this.context = application.run("--spring.profiles.include=dev"); + String property = this.context.getEnvironment().getProperty("my.property"); + assertThat(property).isEqualTo("fromdevpropertiesfile"); + } + @Test public void activeProfilesCanBeConfiguredUsingPlaceholdersResolvedAgainstTheEnvironment() throws Exception {