From c370923596611a2beb937e76cf26320d74637f60 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Wed, 12 Feb 2014 10:52:17 -0800 Subject: [PATCH] Load profile files that include 'spring.profiles' Fix ConfigFileApplicationListener to load profile specific files (*-profile.ext) both as a root document, and again with the profile active. This allows profile specific files to still include a 'spring.profiles' property if they wish. Issue: gh-340 --- .../boot/config/ConfigFileApplicationListener.java | 3 ++- .../boot/env/PropertiesPropertySourceLoader.java | 11 +++++++---- .../boot/env/YamlPropertySourceLoader.java | 2 +- .../config/ConfigFileApplicationListenerTests.java | 11 +++++++++++ .../resources/application-activeprofilewithsubdoc.yml | 2 ++ 5 files changed, 23 insertions(+), 6 deletions(-) create mode 100644 spring-boot/src/test/resources/application-activeprofilewithsubdoc.yml 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