Allow multi-valued spring.config.name

Now accepts CSV list and later values override in the
implied map that is generated.

[Fixes #53785419] [bs-245]
This commit is contained in:
Dave Syer 2013-07-23 11:10:54 +01:00
parent 59a409de2a
commit 4ecfbf68bf
3 changed files with 26 additions and 13 deletions

View File

@ -61,7 +61,7 @@ import org.springframework.util.StringUtils;
*
* <p>
* Alternative locations and names can be specified using
* {@link #setSearchLocations(String[])} and {@link #setName(String)}.
* {@link #setSearchLocations(String[])} and {@link #setNames(String)}.
*
* <p>
* Additional files will also be loaded based on active profiles. For example if a 'web'
@ -89,7 +89,7 @@ public class ConfigFileApplicationContextInitializer implements
private String[] searchLocations = new String[] { "classpath:", "file:./",
"classpath:config/", "file:./config/" };
private String name = "${spring.config.name:application}";
private String names = "${spring.config.name:application}";
private int order = Integer.MIN_VALUE + 10;
@ -145,8 +145,11 @@ public class ConfigFileApplicationContextInitializer implements
List<String> candidates = new ArrayList<String>();
for (String searchLocation : this.searchLocations) {
for (String extension : new String[] { ".properties", ".yml" }) {
String location = searchLocation + this.name + extension;
candidates.add(location);
for (String name : StringUtils
.commaDelimitedListToStringArray(this.names)) {
String location = searchLocation + name + extension;
candidates.add(location);
}
}
}
candidates.add(LOCATION_VARIABLE);
@ -224,10 +227,11 @@ public class ConfigFileApplicationContextInitializer implements
}
/**
* Sets the name of the file that should be loaded (excluding any file extension).
* Sets the names of the files that should be loaded (excluding file extension) as a
* comma separated list. Defaults to "application".
*/
public void setName(String name) {
this.name = name;
public void setNames(String names) {
this.names = names;
}
/**

View File

@ -42,15 +42,23 @@ public class ConfigFileApplicationContextInitializerTests {
@Test
public void loadPropertiesFile() throws Exception {
this.initializer.setName("testproperties");
this.initializer.setNames("testproperties");
this.initializer.initialize(this.context);
String property = this.context.getEnvironment().getProperty("my.property");
assertThat(property, equalTo("frompropertiesfile"));
}
@Test
public void loadTwoPropertiesFiles() throws Exception {
this.initializer.setNames("testproperties,moreproperties");
this.initializer.initialize(this.context);
String property = this.context.getEnvironment().getProperty("my.property");
assertThat(property, equalTo("frommorepropertiesfile"));
}
@Test
public void loadYamlFile() throws Exception {
this.initializer.setName("testyaml");
this.initializer.setNames("testyaml");
this.initializer.initialize(this.context);
String property = this.context.getEnvironment().getProperty("my.property");
assertThat(property, equalTo("fromyamlfile"));
@ -67,7 +75,7 @@ public class ConfigFileApplicationContextInitializerTests {
.addFirst(
new SimpleCommandLinePropertySource(
"--my.property=fromcommandline"));
this.initializer.setName("testproperties");
this.initializer.setNames("testproperties");
this.initializer.initialize(this.context);
String property = this.context.getEnvironment().getProperty("my.property");
assertThat(property, equalTo("fromcommandline"));
@ -75,7 +83,7 @@ public class ConfigFileApplicationContextInitializerTests {
@Test
public void loadPropertiesThenProfileProperties() throws Exception {
this.initializer.setName("enableprofile");
this.initializer.setNames("enableprofile");
this.initializer.initialize(this.context);
String property = this.context.getEnvironment().getProperty("my.property");
assertThat(property, equalTo("fromprofilepropertiesfile"));
@ -83,7 +91,7 @@ public class ConfigFileApplicationContextInitializerTests {
@Test
public void yamlProfiles() throws Exception {
this.initializer.setName("testprofiles");
this.initializer.setNames("testprofiles");
this.context.getEnvironment().setActiveProfiles("dev");
this.initializer.initialize(this.context);
String property = this.context.getEnvironment().getProperty("my.property");
@ -94,7 +102,7 @@ public class ConfigFileApplicationContextInitializerTests {
@Test
public void yamlSetsProfiles() throws Exception {
this.initializer.setName("testsetprofiles");
this.initializer.setNames("testsetprofiles");
this.initializer.initialize(this.context);
String property = this.context.getEnvironment().getProperty("my.property");
assertThat(property, equalTo("fromdevprofile"));

View File

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