Skip to content

Commit c6bf13c

Browse files
committed
Support multi-profile YAML in application-* files
Update ConfigFileEnvironmentPostProcessor to load profile specific sections for all previously processed profiles. Prior to this commit multi-profile YAML files were only loaded from the root `application.yml` file. With the updated logic, an `application-test.yml` file containing the following: someTestProperty: xyz --- spring: profiles: profile1 specificProperty: one Can have the profile sub-document loaded using: -Dspring.profiles.active=test,profile1 Fixes gh-4132
1 parent a899058 commit c6bf13c

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,8 @@ private class Loader {
277277

278278
private Queue<String> profiles;
279279

280+
private List<String> processedProfiles;
281+
280282
private boolean activatedProfiles;
281283

282284
Loader(ConfigurableEnvironment environment, ResourceLoader resourceLoader) {
@@ -288,6 +290,7 @@ private class Loader {
288290
public void load() throws IOException {
289291
this.propertiesLoader = new PropertySourcesLoader();
290292
this.profiles = Collections.asLifoQueue(new LinkedList<String>());
293+
this.processedProfiles = new LinkedList<String>();
291294
this.activatedProfiles = false;
292295
if (this.environment.containsProperty(ACTIVE_PROFILES_PROPERTY)) {
293296
// Any pre-existing active profiles set via property sources (e.g. System
@@ -334,6 +337,7 @@ public void load() throws IOException {
334337
}
335338
}
336339
}
340+
this.processedProfiles.add(profile);
337341
}
338342

339343
addConfigurationProperties(this.propertiesLoader.getPropertySources());
@@ -353,6 +357,12 @@ private void load(String location, String name, String profile)
353357
// Try the profile specific file
354358
loadIntoGroup(group, location + name + "-" + profile + "." + ext,
355359
null);
360+
for (String processedProfile : this.processedProfiles) {
361+
if (processedProfile != null) {
362+
loadIntoGroup(group, location + name + "-"
363+
+ processedProfile + "." + ext, profile);
364+
}
365+
}
356366
// Sometimes people put "spring.profiles: dev" in
357367
// application-dev.yml (gh-340). Arguably we should try and error
358368
// out on that, but we can be kind and load it anyway.
@@ -412,7 +422,6 @@ private void maybeActivateProfiles(Object value) {
412422
}
413423
return;
414424
}
415-
416425
Set<String> profiles = getProfilesForValue(value);
417426
activateProfiles(profiles);
418427
if (profiles.size() > 0) {

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ public void activateProfileFromProfileSpecificProperties() throws Exception {
609609
}
610610

611611
@Test
612-
public void profileSubDocumentInProfileSpecificFile() throws Exception {
612+
public void profileSubDocumentInSameProfileSpecificFile() throws Exception {
613613
// gh-340
614614
SpringApplication application = new SpringApplication(Config.class);
615615
application.setWebEnvironment(false);
@@ -639,6 +639,17 @@ public void bindsSystemPropertyToSpringApplication() throws Exception {
639639
assertThat((Banner.Mode) field.get(this.application), equalTo(Banner.Mode.OFF));
640640
}
641641

642+
@Test
643+
public void profileSubDocumentInDifferentProfileSpecificFile() throws Exception {
644+
// gh-4132
645+
SpringApplication application = new SpringApplication(Config.class);
646+
application.setWebEnvironment(false);
647+
this.context = application.run(
648+
"--spring.profiles.active=activeprofilewithdifferentsubdoc,activeprofilewithdifferentsubdoc2");
649+
String property = this.context.getEnvironment().getProperty("foobar");
650+
assertThat(property, equalTo("baz"));
651+
}
652+
642653
private static Matcher<? super ConfigurableEnvironment> containsPropertySource(
643654
final String sourceName) {
644655
return new TypeSafeDiagnosingMatcher<ConfigurableEnvironment>() {
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
spring.profiles: activeprofilewithdifferentsubdoc2
3+
foobar: baz
4+
---

0 commit comments

Comments
 (0)