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 177c3b375d..e0810bc920 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 @@ -288,7 +288,12 @@ public class ConfigFileApplicationListener implements // Pre-existing active profiles set via Environment.setActiveProfiles() // are additional profiles and config files are allowed to add more if // they want to, so don't call addActiveProfiles() here. - this.profiles.addAll(Arrays.asList(this.environment.getActiveProfiles())); + List list = new ArrayList(Arrays.asList(this.environment + .getActiveProfiles())); + // Reverse them so the order is the same as from getProfilesForValue() + // (last one wins when properties are eventually resolved) + Collections.reverse(list); + this.profiles.addAll(list); } // The default profile for these purposes is represented as null. We add it 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 358de5e987..446cf6736c 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 @@ -272,6 +272,18 @@ public class ConfigFileApplicationListenerTests { assertThat(property, equalTo("fromotherpropertiesfile")); } + @Test + public void twoProfilesFromProperties() throws Exception { + // This should be the effect of calling + // SpringApplication.setAdditionalProfiles("other", "dev") + this.environment.setActiveProfiles("other", "dev"); + this.initializer.onApplicationEvent(this.event); + String property = this.environment.getProperty("my.property"); + // The "dev" profile is activated in SpringApplication so it should take + // precedence over the default profile + assertThat(property, equalTo("fromdevpropertiesfile")); + } + @Test public void loadPropertiesThenProfilePropertiesActivatedInFirst() throws Exception { this.initializer.setSearchNames("enableprofile"); @@ -324,7 +336,7 @@ public class ConfigFileApplicationListenerTests { this.environment.setActiveProfiles("other", "dev"); this.initializer.onApplicationEvent(this.event); String property = this.environment.getProperty("my.property"); - assertThat(property, equalTo("fromotherprofile")); + assertThat(property, equalTo("fromdevprofile")); property = this.environment.getProperty("my.other"); assertThat(property, equalTo("notempty")); } @@ -423,8 +435,9 @@ public class ConfigFileApplicationListenerTests { ConfigurableApplicationContext context = application.run(); String property = context.getEnvironment().getProperty("my.property"); assertThat(property, equalTo("fromspecificlocation")); - assertThat(context.getEnvironment(), containsPropertySource("class path resource " - + "[specificlocation.properties]")); + assertThat(context.getEnvironment(), + containsPropertySource("class path resource " + + "[specificlocation.properties]")); context.close(); } @@ -439,8 +452,9 @@ public class ConfigFileApplicationListenerTests { ConfigurableApplicationContext context = application.run(); String property = context.getEnvironment().getProperty("my.property"); assertThat(property, equalTo("fromspecificlocation")); - assertThat(context.getEnvironment(), containsPropertySource("class path resource " - + "[specificlocation.properties]")); + assertThat(context.getEnvironment(), + containsPropertySource("class path resource " + + "[specificlocation.properties]")); context.close(); } @@ -465,8 +479,9 @@ public class ConfigFileApplicationListenerTests { .run("--spring.profiles.active=myprofile"); String property = context.getEnvironment().getProperty("my.property"); assertThat(property, equalTo("frompropertiesfile")); - assertThat(context.getEnvironment(), containsPropertySource("class path resource " - + "[enableprofile.properties]")); + assertThat(context.getEnvironment(), + containsPropertySource("class path resource " + + "[enableprofile.properties]")); assertThat(context.getEnvironment(), not(containsPropertySource("classpath:/" + "enableprofile-myprofile.properties"))); context.close(); @@ -493,8 +508,9 @@ public class ConfigFileApplicationListenerTests { ConfigurableApplicationContext context = application.run(); String property = context.getEnvironment().getProperty("my.property"); assertThat(property, equalTo("frommorepropertiesfile")); - assertThat(context.getEnvironment(), containsPropertySource("class path resource " - + "[specificlocation.properties]")); + assertThat(context.getEnvironment(), + containsPropertySource("class path resource " + + "[specificlocation.properties]")); context.close(); } diff --git a/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesBindingPostProcessorTests.java b/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesBindingPostProcessorTests.java index baec075537..9171e55a22 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesBindingPostProcessorTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesBindingPostProcessorTests.java @@ -34,7 +34,6 @@ import org.springframework.validation.Validator; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; /** diff --git a/spring-boot/src/test/resources/application-dev.properties b/spring-boot/src/test/resources/application-dev.properties new file mode 100644 index 0000000000..fb5ddcd683 --- /dev/null +++ b/spring-boot/src/test/resources/application-dev.properties @@ -0,0 +1 @@ +my.property=fromdevpropertiesfile \ No newline at end of file