diff --git a/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileEnvironmentPostProcessor.java b/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileEnvironmentPostProcessor.java index 1adeab4cae..3c3676715e 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileEnvironmentPostProcessor.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileEnvironmentPostProcessor.java @@ -277,6 +277,8 @@ public class ConfigFileEnvironmentPostProcessor implements EnvironmentPostProces private Queue profiles; + private List processedProfiles; + private boolean activatedProfiles; Loader(ConfigurableEnvironment environment, ResourceLoader resourceLoader) { @@ -288,6 +290,7 @@ public class ConfigFileEnvironmentPostProcessor implements EnvironmentPostProces public void load() throws IOException { this.propertiesLoader = new PropertySourcesLoader(); this.profiles = Collections.asLifoQueue(new LinkedList()); + this.processedProfiles = new LinkedList(); this.activatedProfiles = false; if (this.environment.containsProperty(ACTIVE_PROFILES_PROPERTY)) { // Any pre-existing active profiles set via property sources (e.g. System @@ -334,6 +337,7 @@ public class ConfigFileEnvironmentPostProcessor implements EnvironmentPostProces } } } + this.processedProfiles.add(profile); } addConfigurationProperties(this.propertiesLoader.getPropertySources()); @@ -353,6 +357,12 @@ public class ConfigFileEnvironmentPostProcessor implements EnvironmentPostProces // 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. @@ -412,7 +422,6 @@ public class ConfigFileEnvironmentPostProcessor implements EnvironmentPostProces } return; } - Set profiles = getProfilesForValue(value); activateProfiles(profiles); if (profiles.size() > 0) { diff --git a/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileEnvironmentPostProcessorTests.java b/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileEnvironmentPostProcessorTests.java index c9ae2bd971..250495cd98 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileEnvironmentPostProcessorTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileEnvironmentPostProcessorTests.java @@ -609,7 +609,7 @@ public class ConfigFileEnvironmentPostProcessorTests { } @Test - public void profileSubDocumentInProfileSpecificFile() throws Exception { + public void profileSubDocumentInSameProfileSpecificFile() throws Exception { // gh-340 SpringApplication application = new SpringApplication(Config.class); application.setWebEnvironment(false); @@ -639,6 +639,17 @@ public class ConfigFileEnvironmentPostProcessorTests { 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 containsPropertySource( final String sourceName) { return new TypeSafeDiagnosingMatcher() { diff --git a/spring-boot/src/test/resources/application-activeprofilewithdifferentsubdoc.yml b/spring-boot/src/test/resources/application-activeprofilewithdifferentsubdoc.yml new file mode 100644 index 0000000000..934c8c0aa7 --- /dev/null +++ b/spring-boot/src/test/resources/application-activeprofilewithdifferentsubdoc.yml @@ -0,0 +1,4 @@ +--- +spring.profiles: activeprofilewithdifferentsubdoc2 +foobar: baz +---