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
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<? super ConfigurableEnvironment> containsProperySource(
|
||||
final String sourceName) {
|
||||
return new TypeSafeDiagnosingMatcher<ConfigurableEnvironment>() {
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
spring.profiles: activeprofilewithsubdoc
|
||||
foobar: baz
|
||||
Reference in New Issue
Block a user