Skip to content

Commit 378c4c9

Browse files
mbhavephilwebb
authored andcommitted
Stop included profiles overriding active profiles
Update `ConfigFileApplicationListener` so that `spring.profiles.include` properties do not override higher priority active profiles. This commit also changes when profiles get added to the environment. Profiles are now added to the environment at the time of profile processing so that they get logged in the order that they are processed. Closes gh-13151
1 parent 60a4d67 commit 378c4c9

File tree

3 files changed

+39
-21
lines changed

3 files changed

+39
-21
lines changed

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

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,9 @@ public void load() {
328328
initializeProfiles();
329329
while (!this.profiles.isEmpty()) {
330330
Profile profile = this.profiles.poll();
331+
if (profile != null && !profile.isDefaultProfile()) {
332+
addProfileToEnvironment(profile.getName());
333+
}
331334
load(profile, this::getPositiveProfileFilter,
332335
addToLoaded(MutablePropertySources::addLast, false));
333336
this.processedProfiles.add(profile);
@@ -347,15 +350,13 @@ private void initializeProfiles() {
347350
// first so that it is processed first and has lowest priority.
348351
this.profiles.add(null);
349352
Set<Profile> activatedViaProperty = getProfilesActivatedViaProperty();
350-
processOtherActiveProfiles(activatedViaProperty);
353+
this.profiles.addAll(getOtherActiveProfiles(activatedViaProperty));
351354
// Any pre-existing active profiles set via property sources (e.g.
352-
// System
353-
// properties) take precedence over those added in config files.
355+
// System properties) take precedence over those added in config files.
354356
addActiveProfiles(activatedViaProperty);
355357
if (this.profiles.size() == 1) { // only has null profile
356358
for (String defaultProfileName : this.environment.getDefaultProfiles()) {
357-
ConfigFileApplicationListener.Profile defaultProfile = new ConfigFileApplicationListener.Profile(
358-
defaultProfileName, true);
359+
Profile defaultProfile = new Profile(defaultProfileName, true);
359360
this.profiles.add(defaultProfile);
360361
}
361362
}
@@ -373,19 +374,22 @@ private Set<Profile> getProfilesActivatedViaProperty() {
373374
return activeProfiles;
374375
}
375376

376-
private void processOtherActiveProfiles(Set<Profile> activatedViaProperty) {
377-
List<Profile> otherActiveProfiles = Arrays
378-
.stream(this.environment.getActiveProfiles()).map(Profile::new)
379-
.filter((o) -> !activatedViaProperty.contains(o))
377+
private List<Profile> getOtherActiveProfiles(Set<Profile> activatedViaProperty) {
378+
return Arrays.stream(this.environment.getActiveProfiles()).map(Profile::new)
379+
.filter((profile) -> !activatedViaProperty.contains(profile))
380380
.collect(Collectors.toList());
381-
this.profiles.addAll(otherActiveProfiles);
382381
}
383382

384383
void addActiveProfiles(Set<Profile> profiles) {
385-
if (this.activatedProfiles || profiles.isEmpty()) {
384+
if (profiles.isEmpty()) {
385+
return;
386+
}
387+
if (this.activatedProfiles) {
388+
this.logger.debug("Profiles already activated, '" + profiles
389+
+ "' will not be applied");
386390
return;
387391
}
388-
addProfiles(profiles);
392+
this.profiles.addAll(profiles);
389393
if (this.logger.isDebugEnabled()) {
390394
this.logger.debug("Activated activeProfiles "
391395
+ StringUtils.collectionToCommaDelimitedString(profiles));
@@ -394,13 +398,6 @@ void addActiveProfiles(Set<Profile> profiles) {
394398
removeUnprocessedDefaultProfiles();
395399
}
396400

397-
void addProfiles(Set<Profile> profiles) {
398-
for (Profile profile : profiles) {
399-
this.profiles.add(profile);
400-
addProfileToEnvironment(profile.getName());
401-
}
402-
}
403-
404401
private void removeUnprocessedDefaultProfiles() {
405402
this.profiles.removeIf(
406403
(profile) -> (profile != null && profile.isDefaultProfile()));
@@ -526,7 +523,7 @@ private void load(PropertySourceLoader loader, String location, Profile profile,
526523
for (Document document : documents) {
527524
if (filter.match(document)) {
528525
addActiveProfiles(document.getActiveProfiles());
529-
addProfiles(document.getIncludeProfiles());
526+
addIncludedProfiles(document.getIncludeProfiles());
530527
loaded.add(document);
531528
}
532529
}
@@ -542,6 +539,13 @@ private void load(PropertySourceLoader loader, String location, Profile profile,
542539
}
543540
}
544541

542+
private void addIncludedProfiles(Set<Profile> includeProfiles) {
543+
LinkedList<Profile> existingProfiles = new LinkedList<>(this.profiles);
544+
this.profiles.clear();
545+
this.profiles.addAll(includeProfiles);
546+
this.profiles.addAll(existingProfiles);
547+
}
548+
545549
private List<Document> loadDocuments(PropertySourceLoader loader, String name,
546550
Resource resource) throws IOException {
547551
DocumentsCacheKey cacheKey = new DocumentsCacheKey(loader, resource);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public void yamlProfileCascading() {
9494
application.setWebApplicationType(WebApplicationType.NONE);
9595
String configName = "--spring.config.name=cascadingprofiles";
9696
this.context = application.run(configName);
97-
assertVersionProperty(this.context, "E", "A", "B", "C", "E", "D");
97+
assertVersionProperty(this.context, "D", "A", "C", "E", "B", "D");
9898
assertThat(this.context.getEnvironment().getProperty("not-a")).isNull();
9999
assertThat(this.context.getEnvironment().getProperty("not-b")).isNull();
100100
assertThat(this.context.getEnvironment().getProperty("not-c")).isNull();

spring-boot-project/spring-boot/src/test/resources/cascadingprofiles.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ spring:
1212
include:
1313
- C
1414
- E
15+
version: A
1516

1617
---
1718
spring.profiles: B
@@ -21,6 +22,19 @@ spring:
2122
include:
2223
- D
2324
- E
25+
version: B
26+
27+
---
28+
spring.profiles: C
29+
30+
version: C
31+
32+
33+
---
34+
spring.profiles: D
35+
36+
version: D
37+
2438

2539
---
2640
spring.profiles: E

0 commit comments

Comments
 (0)