Skip to content

Commit

Permalink
Support multi-profile YAML in application-* files
Browse files Browse the repository at this point in the history
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
  • Loading branch information
philwebb committed Oct 12, 2015
1 parent a899058 commit c6bf13c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,8 @@ private class Loader {

private Queue<String> profiles;

private List<String> processedProfiles;

private boolean activatedProfiles;

Loader(ConfigurableEnvironment environment, ResourceLoader resourceLoader) {
Expand All @@ -288,6 +290,7 @@ private class Loader {
public void load() throws IOException {
this.propertiesLoader = new PropertySourcesLoader();
this.profiles = Collections.asLifoQueue(new LinkedList<String>());
this.processedProfiles = new LinkedList<String>();
this.activatedProfiles = false;
if (this.environment.containsProperty(ACTIVE_PROFILES_PROPERTY)) {
// Any pre-existing active profiles set via property sources (e.g. System
Expand Down Expand Up @@ -334,6 +337,7 @@ public void load() throws IOException {
}
}
}
this.processedProfiles.add(profile);
}

addConfigurationProperties(this.propertiesLoader.getPropertySources());
Expand All @@ -353,6 +357,12 @@ private void load(String location, String name, String profile)
// Try the profile specific file
loadIntoGroup(group, location + name + "-" + profile + "." + ext,
null);
for (String processedProfile : this.processedProfiles) {
if (processedProfile != null) {
loadIntoGroup(group, location + name + "-"
+ processedProfile + "." + ext, profile);
}
}
// Sometimes people put "spring.profiles: dev" in
// application-dev.yml (gh-340). Arguably we should try and error
// out on that, but we can be kind and load it anyway.
Expand Down Expand Up @@ -412,7 +422,6 @@ private void maybeActivateProfiles(Object value) {
}
return;
}

Set<String> profiles = getProfilesForValue(value);
activateProfiles(profiles);
if (profiles.size() > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ public void activateProfileFromProfileSpecificProperties() throws Exception {
}

@Test
public void profileSubDocumentInProfileSpecificFile() throws Exception {
public void profileSubDocumentInSameProfileSpecificFile() throws Exception {
// gh-340
SpringApplication application = new SpringApplication(Config.class);
application.setWebEnvironment(false);
Expand Down Expand Up @@ -639,6 +639,17 @@ public void bindsSystemPropertyToSpringApplication() throws Exception {
assertThat((Banner.Mode) field.get(this.application), equalTo(Banner.Mode.OFF));
}

@Test
public void profileSubDocumentInDifferentProfileSpecificFile() throws Exception {
// gh-4132
SpringApplication application = new SpringApplication(Config.class);
application.setWebEnvironment(false);
this.context = application.run(
"--spring.profiles.active=activeprofilewithdifferentsubdoc,activeprofilewithdifferentsubdoc2");
String property = this.context.getEnvironment().getProperty("foobar");
assertThat(property, equalTo("baz"));
}

private static Matcher<? super ConfigurableEnvironment> containsPropertySource(
final String sourceName) {
return new TypeSafeDiagnosingMatcher<ConfigurableEnvironment>() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
spring.profiles: activeprofilewithdifferentsubdoc2
foobar: baz
---

0 comments on commit c6bf13c

Please sign in to comment.