diff --git a/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java b/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java index 5aed2fb0216..106bd3a2cea 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java @@ -305,11 +305,8 @@ public class ConfigFileApplicationListener implements private PropertySource load(String resourceLocation, String profile) throws IOException { Resource resource = this.resourceLoader.getResource(resourceLocation); - if (resource != null && resource.exists()) { + if (resource != null) { String name = "applicationConfig: " + resource.getDescription(); - if (StringUtils.hasLength(profile)) { - name += " " + profile; - } PropertySource propertySource = this.propertiesLoader.load(resource, name, profile); if (propertySource != null) { diff --git a/spring-boot/src/main/java/org/springframework/boot/env/PropertySourcesLoader.java b/spring-boot/src/main/java/org/springframework/boot/env/PropertySourcesLoader.java index 742f8245c5f..25e9fd55e1b 100644 --- a/spring-boot/src/main/java/org/springframework/boot/env/PropertySourcesLoader.java +++ b/spring-boot/src/main/java/org/springframework/boot/env/PropertySourcesLoader.java @@ -27,6 +27,7 @@ import org.springframework.core.env.PropertySource; import org.springframework.core.io.Resource; import org.springframework.core.io.support.SpringFactoriesLoader; import org.springframework.util.Assert; +import org.springframework.util.StringUtils; /** * Utiltiy that can be used to {@link MutablePropertySources} using @@ -70,7 +71,7 @@ public class PropertySourcesLoader { */ public PropertySource load(Resource resource, String name, String profile) throws IOException { - if (resource != null && resource.exists()) { + if (isFile(resource)) { name = generatePropertySourceName(resource, name, profile); for (PropertySourceLoader loader : this.loaders) { if (canLoadFileExtension(loader, resource)) { @@ -83,6 +84,13 @@ public class PropertySourcesLoader { return null; } + private boolean isFile(Resource resource) { + return resource != null + && resource.exists() + && StringUtils.hasText(StringUtils.getFilenameExtension(resource + .getFilename())); + } + private String generatePropertySourceName(Resource resource, String name, String profile) { if (name == null) { diff --git a/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java b/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java index 782def87429..3ea7a2a8645 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java @@ -17,7 +17,10 @@ package org.springframework.boot.context.config; import java.lang.reflect.Field; +import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; +import java.util.List; import org.hamcrest.Description; import org.hamcrest.Matcher; @@ -45,6 +48,7 @@ import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.nullValue; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; /** @@ -178,6 +182,20 @@ public class ConfigFileApplicationListenerTests { String property = this.environment.getProperty("my.property"); assertThat(Arrays.asList(this.environment.getActiveProfiles()), contains("dev")); assertThat(property, equalTo("fromdevprofile")); + ConfigurationPropertySources propertySource = (ConfigurationPropertySources) this.environment + .getPropertySources().get("applicationConfigurationProperties"); + Collection> sources = propertySource + .getSource(); + assertEquals(2, sources.size()); + List names = new ArrayList(); + for (org.springframework.core.env.PropertySource source : sources) { + names.add(source.getName()); + } + assertThat( + names, + contains( + "applicationConfig: class path resource [testsetprofiles.yml]#dev", + "applicationConfig: class path resource [testsetprofiles.yml]")); } @Test @@ -189,6 +207,30 @@ public class ConfigFileApplicationListenerTests { assertThat(this.environment.getActiveProfiles(), equalTo(new String[] { "prod" })); } + @Test + public void yamlProfileOrdering() throws Exception { + this.initializer.setSearchNames("threeprofiles"); + this.environment.setActiveProfiles("A", "C"); + this.initializer.onApplicationEvent(this.event); + assertThat(this.environment.getProperty("version"), equalTo("C")); + } + + @Test + public void yamlProfileOrderingReverse() throws Exception { + this.initializer.setSearchNames("threeprofiles"); + this.environment.setActiveProfiles("C", "A"); + this.initializer.onApplicationEvent(this.event); + assertThat(this.environment.getProperty("version"), equalTo("A")); + } + + @Test + public void yamlProfileOrderingOverride() throws Exception { + this.initializer.setSearchNames("threeprofiles-with-override"); + this.environment.setActiveProfiles("C", "A"); + this.initializer.onApplicationEvent(this.event); + assertThat(this.environment.getProperty("version"), equalTo("B")); + } + @Test public void specificNameAndProfileFromExistingSource() throws Exception { EnvironmentTestUtils.addEnvironment(this.environment, diff --git a/spring-boot/src/test/resources/threeprofiles-with-override.yml b/spring-boot/src/test/resources/threeprofiles-with-override.yml new file mode 100644 index 00000000000..cd92c27ab4a --- /dev/null +++ b/spring-boot/src/test/resources/threeprofiles-with-override.yml @@ -0,0 +1,12 @@ +--- +spring.profiles.active: B +--- +spring.profiles: A +version: A +--- +spring.profiles: B +version: B +--- +spring.profiles: C +version: C +--- \ No newline at end of file diff --git a/spring-boot/src/test/resources/threeprofiles.yml b/spring-boot/src/test/resources/threeprofiles.yml new file mode 100644 index 00000000000..3c0a9e151fe --- /dev/null +++ b/spring-boot/src/test/resources/threeprofiles.yml @@ -0,0 +1,11 @@ +--- +--- +spring.profiles: A +version: A +--- +spring.profiles: B +version: B +--- +spring.profiles: C +version: C +--- \ No newline at end of file