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
pull/352/merge
Phillip Webb 11 years ago
parent 89283e46b8
commit c370923596

@ -270,8 +270,9 @@ public class ConfigFileApplicationListener implements
if (locationPropertySource == null) { if (locationPropertySource == null) {
for (String ext : this.propertiesLoader.getAllFileExtensions()) { for (String ext : this.propertiesLoader.getAllFileExtensions()) {
if (profile != null) { 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, null);
load(location + name + "-" + profile + "." + ext, profile);
} }
// Try the profile (if any) specific section of the normal file // Try the profile (if any) specific section of the normal file
load(location + name + "." + ext, profile); load(location + name + "." + ext, profile);

@ -17,6 +17,7 @@
package org.springframework.boot.env; package org.springframework.boot.env;
import java.io.IOException; import java.io.IOException;
import java.util.Properties;
import org.springframework.core.env.PropertiesPropertySource; import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.PropertySource; import org.springframework.core.env.PropertySource;
@ -39,10 +40,12 @@ public class PropertiesPropertySourceLoader implements PropertySourceLoader {
@Override @Override
public PropertySource<?> load(String name, Resource resource, String profile) public PropertySource<?> load(String name, Resource resource, String profile)
throws IOException { throws IOException {
if (profile != null) { if (profile == null) {
return null; Properties properties = PropertiesLoaderUtils.loadProperties(resource);
if (!properties.isEmpty()) {
return new PropertiesPropertySource(name, properties);
}
} }
return new PropertiesPropertySource(name, return null;
PropertiesLoaderUtils.loadProperties(resource));
} }
} }

@ -54,7 +54,7 @@ public class YamlPropertySourceLoader implements PropertySourceLoader {
} }
factory.setResources(new Resource[] { resource }); factory.setResources(new Resource[] { resource });
Properties properties = factory.getObject(); Properties properties = factory.getObject();
if (profile == null || !properties.isEmpty()) { if (!properties.isEmpty()) {
return new PropertiesPropertySource(name, properties); return new PropertiesPropertySource(name, properties);
} }
} }

@ -340,6 +340,17 @@ public class ConfigFileApplicationListenerTests {
assertThat(context.getEnvironment(), not(acceptsProfiles("missing"))); 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<? super ConfigurableEnvironment> containsProperySource( private static Matcher<? super ConfigurableEnvironment> containsProperySource(
final String sourceName) { final String sourceName) {
return new TypeSafeDiagnosingMatcher<ConfigurableEnvironment>() { return new TypeSafeDiagnosingMatcher<ConfigurableEnvironment>() {

@ -0,0 +1,2 @@
spring.profiles: activeprofilewithsubdoc
foobar: baz
Loading…
Cancel
Save