Reverse order of active profiles before processing

This change harmonizes the profile ordering between spring.profiles.active
(where last one already wins) and SpringApplication.setAdditionalProfiles()
(where the first one has been winning in 1.0.x). Last one wins is the
correct strategy since it mimics a map merge, and also matches the behaviour
of file ordering already defined in spring.config.location and
spring.config.name.

Fixes gh-645
This commit is contained in:
Dave Syer 2014-05-01 11:11:16 +01:00
parent fe97e66e2a
commit 3a6d4efe28
4 changed files with 32 additions and 11 deletions

View File

@ -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<String> list = new ArrayList<String>(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

View File

@ -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();
}

View File

@ -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;
/**

View File

@ -0,0 +1 @@
my.property=fromdevpropertiesfile