diff --git a/spring-boot/src/main/java/org/springframework/boot/config/ConfigFileApplicationListener.java b/spring-boot/src/main/java/org/springframework/boot/config/ConfigFileApplicationListener.java index 68a2662625..64fc6ec9c9 100644 --- a/spring-boot/src/main/java/org/springframework/boot/config/ConfigFileApplicationListener.java +++ b/spring-boot/src/main/java/org/springframework/boot/config/ConfigFileApplicationListener.java @@ -270,8 +270,9 @@ public class ConfigFileApplicationListener implements if (locationPropertySource == null) { for (String ext : this.propertiesLoader.getAllFileExtensions()) { if (profile != null) { - // Try the profile specific file (with a null profile section) + // Try the profile specific file load(location + name + "-" + profile + "." + ext, null); + load(location + name + "-" + profile + "." + ext, profile); } // Try the profile (if any) specific section of the normal file load(location + name + "." + ext, profile); diff --git a/spring-boot/src/main/java/org/springframework/boot/env/PropertiesPropertySourceLoader.java b/spring-boot/src/main/java/org/springframework/boot/env/PropertiesPropertySourceLoader.java index dbebdab085..ec03d11cdb 100644 --- a/spring-boot/src/main/java/org/springframework/boot/env/PropertiesPropertySourceLoader.java +++ b/spring-boot/src/main/java/org/springframework/boot/env/PropertiesPropertySourceLoader.java @@ -17,6 +17,7 @@ package org.springframework.boot.env; import java.io.IOException; +import java.util.Properties; import org.springframework.core.env.PropertiesPropertySource; import org.springframework.core.env.PropertySource; @@ -39,10 +40,12 @@ public class PropertiesPropertySourceLoader implements PropertySourceLoader { @Override public PropertySource load(String name, Resource resource, String profile) throws IOException { - if (profile != null) { - return null; + if (profile == null) { + Properties properties = PropertiesLoaderUtils.loadProperties(resource); + if (!properties.isEmpty()) { + return new PropertiesPropertySource(name, properties); + } } - return new PropertiesPropertySource(name, - PropertiesLoaderUtils.loadProperties(resource)); + return null; } } diff --git a/spring-boot/src/main/java/org/springframework/boot/env/YamlPropertySourceLoader.java b/spring-boot/src/main/java/org/springframework/boot/env/YamlPropertySourceLoader.java index 2fa2a1a039..2c88b55e65 100644 --- a/spring-boot/src/main/java/org/springframework/boot/env/YamlPropertySourceLoader.java +++ b/spring-boot/src/main/java/org/springframework/boot/env/YamlPropertySourceLoader.java @@ -54,7 +54,7 @@ public class YamlPropertySourceLoader implements PropertySourceLoader { } factory.setResources(new Resource[] { resource }); Properties properties = factory.getObject(); - if (profile == null || !properties.isEmpty()) { + if (!properties.isEmpty()) { return new PropertiesPropertySource(name, properties); } } diff --git a/spring-boot/src/test/java/org/springframework/boot/config/ConfigFileApplicationListenerTests.java b/spring-boot/src/test/java/org/springframework/boot/config/ConfigFileApplicationListenerTests.java index d6e69f3ad9..67adbd5df6 100644 --- a/spring-boot/src/test/java/org/springframework/boot/config/ConfigFileApplicationListenerTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/config/ConfigFileApplicationListenerTests.java @@ -340,6 +340,17 @@ public class ConfigFileApplicationListenerTests { assertThat(context.getEnvironment(), not(acceptsProfiles("missing"))); } + @Test + public void profileSubDocumentInProfileSpecificFile() throws Exception { + // gh-340 + SpringApplication application = new SpringApplication(Config.class); + application.setWebEnvironment(false); + ConfigurableApplicationContext context = application + .run("--spring.profiles.active=activeprofilewithsubdoc"); + String property = context.getEnvironment().getProperty("foobar"); + assertThat(property, equalTo("baz")); + } + private static Matcher containsProperySource( final String sourceName) { return new TypeSafeDiagnosingMatcher() { diff --git a/spring-boot/src/test/resources/application-activeprofilewithsubdoc.yml b/spring-boot/src/test/resources/application-activeprofilewithsubdoc.yml new file mode 100644 index 0000000000..0d1112894a --- /dev/null +++ b/spring-boot/src/test/resources/application-activeprofilewithsubdoc.yml @@ -0,0 +1,2 @@ +spring.profiles: activeprofilewithsubdoc +foobar: baz