From 06bb6bd1e1b6c8c44e2ebb83f70bc18a6573fc78 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Wed, 21 Oct 2015 11:43:50 -0400 Subject: [PATCH] Fix logic affecting files loaded The problem fixed here is that the Loader keeps track of the profiles it is going to look at for loading files, but ignores any that were already active in the Environment if the listener is called initially with spring.profiles.active not empty. Closes gh-4261 --- .../config/ConfigFileApplicationListener.java | 20 +++++++++---------- .../ConfigFileApplicationListenerTests.java | 12 +++++++++++ 2 files changed, 21 insertions(+), 11 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 57f12bdcf82..b3c8ea160aa 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 @@ -314,17 +314,15 @@ public class ConfigFileApplicationListener maybeActivateProfiles( this.environment.getProperty(ACTIVE_PROFILES_PROPERTY)); } - else { - // Pre-existing active profiles set via Environment.setActiveProfiles() - // are additional profiles and config files are allowed to add more if - // they want to, so don't call addActiveProfiles() here. - List list = new ArrayList( - Arrays.asList(this.environment.getActiveProfiles())); - // Reverse them so the order is the same as from getProfilesForValue() - // (last one wins when properties are eventually resolved) - Collections.reverse(list); - this.profiles.addAll(list); - } + // Pre-existing active profiles set via Environment.setActiveProfiles() + // are additional profiles and config files are allowed to add more if + // they want to, so don't call addActiveProfiles() here. + List list = new ArrayList( + Arrays.asList(this.environment.getActiveProfiles())); + // Reverse them so the order is the same as from getProfilesForValue() + // (last one wins when properties are eventually resolved) + Collections.reverse(list); + this.profiles.addAll(list); // The default profile for these purposes is represented as null. We add it // last so that it is first out of the queue (active profiles will then 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 474d4772723..a273cdb5756 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 @@ -341,6 +341,18 @@ public class ConfigFileApplicationListenerTests { assertThat(property, equalTo("fromprofilepropertiesfile")); } + @Test + public void profilesAddedToEnvironmentAndViaProperty() throws Exception { + EnvironmentTestUtils.addEnvironment(this.environment, + "spring.profiles.active:foo"); + this.environment.addActiveProfile("dev"); + this.initializer.onApplicationEvent(this.event); + assertThat(this.environment.getActiveProfiles(), + equalTo(new String[] { "foo", "dev" })); + assertThat(this.environment.getProperty("my.property"), + equalTo("fromdevpropertiesfile")); + } + @Test public void yamlProfiles() throws Exception { this.initializer.setSearchNames("testprofiles");