diff --git a/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java b/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java index 2197a3265a5..a59d19f3b2c 100644 --- a/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java +++ b/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java @@ -388,8 +388,8 @@ public class SpringApplication { * the above for fine-grained control over property sources or profiles, respectively. * @param environment this application's environment * @param args arguments passed to the {@code run} method - * @see #configurePropertySources(ConfigurableEnvironment, String[]) * @see #configureProfiles(ConfigurableEnvironment, String[]) + * @see #configurePropertySources(ConfigurableEnvironment, String[]) */ protected void configureEnvironment(ConfigurableEnvironment environment, String[] args) { configurePropertySources(environment, args); 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 225833c2fba..f2a1bc6aa6d 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 @@ -285,6 +285,9 @@ public class ConfigFileApplicationListener implements this.profiles.addAll(Arrays.asList(this.environment.getActiveProfiles())); } + // The default profile for these purposes is represented as null. We add it + // last so that it is first out (active profiles will then override any + // settings in the defaults when the list is reversed later). this.profiles.add(null); while (!this.profiles.isEmpty()) { diff --git a/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java b/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java index 23d6f62b954..cd808d68301 100644 --- a/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java @@ -296,9 +296,22 @@ public class SpringApplicationTests { application.setAdditionalProfiles("foo"); ConfigurableEnvironment environment = new StandardEnvironment(); application.setEnvironment(environment); - application.run("--spring.profiles.active=bar"); + application.run("--spring.profiles.active=bar,spam"); // Command line should always come last - assertArrayEquals(new String[] { "foo", "bar" }, environment.getActiveProfiles()); + assertArrayEquals(new String[] { "foo", "bar", "spam" }, + environment.getActiveProfiles()); + } + + @Test + public void addProfilesOrderWithProperties() throws Exception { + SpringApplication application = new SpringApplication(ExampleConfig.class); + application.setWebEnvironment(false); + application.setAdditionalProfiles("other"); + ConfigurableEnvironment environment = new StandardEnvironment(); + application.setEnvironment(environment); + application.run(); + // Active profile should win over default + assertEquals("fromotherpropertiesfile", environment.getProperty("my.property")); } @Test 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 43d661ad4ac..a36ff188796 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 @@ -189,7 +189,20 @@ public class ConfigFileApplicationListenerTests { } @Test - public void loadPropertiesThenProfileProperties() throws Exception { + public void loadPropertiesThenProfilePropertiesActivatedInSpringApplication() + throws Exception { + // This should be the effect of calling + // SpringApplication.setAdditionalProfiles("other") + this.environment.setActiveProfiles("other"); + this.initializer.onApplicationEvent(this.event); + String property = this.environment.getProperty("my.property"); + // The "other" profile is activated in SpringApplication so it should take + // precedence over the default profile + assertThat(property, equalTo("fromotherpropertiesfile")); + } + + @Test + public void loadPropertiesThenProfilePropertiesActivatedInFirst() throws Exception { this.initializer.setSearchNames("enableprofile"); this.initializer.onApplicationEvent(this.event); String property = this.environment.getProperty("my.property");