Skip to content

Commit 06bb6bd

Browse files
Dave Syersnicoll
authored andcommitted
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
1 parent 2e2ebeb commit 06bb6bd

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -314,17 +314,15 @@ public void load() throws IOException {
314314
maybeActivateProfiles(
315315
this.environment.getProperty(ACTIVE_PROFILES_PROPERTY));
316316
}
317-
else {
318-
// Pre-existing active profiles set via Environment.setActiveProfiles()
319-
// are additional profiles and config files are allowed to add more if
320-
// they want to, so don't call addActiveProfiles() here.
321-
List<String> list = new ArrayList<String>(
322-
Arrays.asList(this.environment.getActiveProfiles()));
323-
// Reverse them so the order is the same as from getProfilesForValue()
324-
// (last one wins when properties are eventually resolved)
325-
Collections.reverse(list);
326-
this.profiles.addAll(list);
327-
}
317+
// Pre-existing active profiles set via Environment.setActiveProfiles()
318+
// are additional profiles and config files are allowed to add more if
319+
// they want to, so don't call addActiveProfiles() here.
320+
List<String> list = new ArrayList<String>(
321+
Arrays.asList(this.environment.getActiveProfiles()));
322+
// Reverse them so the order is the same as from getProfilesForValue()
323+
// (last one wins when properties are eventually resolved)
324+
Collections.reverse(list);
325+
this.profiles.addAll(list);
328326

329327
// The default profile for these purposes is represented as null. We add it
330328
// last so that it is first out of the queue (active profiles will then

spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,18 @@ public void profilePropertiesUsedInPlaceholders() throws Exception {
341341
assertThat(property, equalTo("fromprofilepropertiesfile"));
342342
}
343343

344+
@Test
345+
public void profilesAddedToEnvironmentAndViaProperty() throws Exception {
346+
EnvironmentTestUtils.addEnvironment(this.environment,
347+
"spring.profiles.active:foo");
348+
this.environment.addActiveProfile("dev");
349+
this.initializer.onApplicationEvent(this.event);
350+
assertThat(this.environment.getActiveProfiles(),
351+
equalTo(new String[] { "foo", "dev" }));
352+
assertThat(this.environment.getProperty("my.property"),
353+
equalTo("fromdevpropertiesfile"));
354+
}
355+
344356
@Test
345357
public void yamlProfiles() throws Exception {
346358
this.initializer.setSearchNames("testprofiles");

0 commit comments

Comments
 (0)