Skip to content

Commit 7e2494e

Browse files
committed
Set environment active profiles according to processing order
Fixes gh-13965
1 parent e4442f4 commit 7e2494e

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ public void load() {
337337
addToLoaded(MutablePropertySources::addLast, false));
338338
this.processedProfiles.add(profile);
339339
}
340+
resetEnvironmentProfiles(this.processedProfiles);
340341
load(null, this::getNegativeProfileFilter,
341342
addToLoaded(MutablePropertySources::addFirst, true));
342343
addLoadedPropertySources();
@@ -673,6 +674,21 @@ private Set<String> asResolvedSet(String value, String fallback) {
673674
return new LinkedHashSet<>(list);
674675
}
675676

677+
/**
678+
* This ensures that the order of active profiles in the {@link Environment}
679+
* matches the order in which the profiles were processed.
680+
* @param processedProfiles the processed profiles
681+
*/
682+
private void resetEnvironmentProfiles(List<Profile> processedProfiles) {
683+
String[] names = processedProfiles.stream().filter((profile) -> {
684+
if (profile != null && !profile.isDefaultProfile()) {
685+
return true;
686+
}
687+
return false;
688+
}).map(Profile::getName).toArray(String[]::new);
689+
this.environment.setActiveProfiles(names);
690+
}
691+
676692
private void addLoadedPropertySources() {
677693
MutablePropertySources destination = this.environment.getPropertySources();
678694
List<MutablePropertySources> loaded = new ArrayList<>(this.loaded.values());

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ public void profilesAddedToEnvironmentViaActiveAndIncludeProperty() {
406406
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
407407
"spring.profiles.active=dev", "spring.profiles.include=other");
408408
this.initializer.postProcessEnvironment(this.environment, this.application);
409-
assertThat(this.environment.getActiveProfiles()).contains("dev", "other");
409+
assertThat(this.environment.getActiveProfiles()).containsExactly("other", "dev");
410410
assertThat(this.environment.getProperty("my.property"))
411411
.isEqualTo("fromdevpropertiesfile");
412412
validateProfilePrecedence(null, "other", "dev");

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,20 @@ public void yamlProfileCascadingOverrideProfilesA() {
116116
assertThat(this.context.getEnvironment().getProperty("not-e")).isNull();
117117
}
118118

119+
@Test
120+
public void yamlProfileCascadingMultipleActiveProfilesViaPropertiesShouldPreserveOrder() {
121+
SpringApplication application = new SpringApplication(Config.class);
122+
application.setWebApplicationType(WebApplicationType.NONE);
123+
String configName = "--spring.config.name=cascadingprofiles";
124+
this.context = application.run(configName, "--spring.profiles.active=A,B");
125+
assertVersionProperty(this.context, "D", "A", "C", "E", "B", "D");
126+
assertThat(this.context.getEnvironment().getProperty("not-a")).isNull();
127+
assertThat(this.context.getEnvironment().getProperty("not-b")).isNull();
128+
assertThat(this.context.getEnvironment().getProperty("not-c")).isNull();
129+
assertThat(this.context.getEnvironment().getProperty("not-d")).isNull();
130+
assertThat(this.context.getEnvironment().getProperty("not-e")).isNull();
131+
}
132+
119133
@Test
120134
public void yamlProfileCascadingOverrideProfilesB() {
121135
SpringApplication application = new SpringApplication(Config.class);

0 commit comments

Comments
 (0)