From 4ecfbf68bfa2c09091d5954967b3614e63b2961f Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Tue, 23 Jul 2013 11:10:54 +0100 Subject: [PATCH] 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] --- ...nfigFileApplicationContextInitializer.java | 18 ++++++++++------- ...ileApplicationContextInitializerTests.java | 20 +++++++++++++------ .../test/resources/moreproperties.properties | 1 + 3 files changed, 26 insertions(+), 13 deletions(-) create mode 100644 spring-bootstrap/src/test/resources/moreproperties.properties diff --git a/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/initializer/ConfigFileApplicationContextInitializer.java b/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/initializer/ConfigFileApplicationContextInitializer.java index c944a1f9ff5..13ce8898bf9 100644 --- a/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/initializer/ConfigFileApplicationContextInitializer.java +++ b/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/initializer/ConfigFileApplicationContextInitializer.java @@ -61,7 +61,7 @@ import org.springframework.util.StringUtils; * *

* Alternative locations and names can be specified using - * {@link #setSearchLocations(String[])} and {@link #setName(String)}. + * {@link #setSearchLocations(String[])} and {@link #setNames(String)}. * *

* 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 candidates = new ArrayList(); 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; } /** diff --git a/spring-bootstrap/src/test/java/org/springframework/bootstrap/context/initializer/ConfigFileApplicationContextInitializerTests.java b/spring-bootstrap/src/test/java/org/springframework/bootstrap/context/initializer/ConfigFileApplicationContextInitializerTests.java index 1d539a4fb7f..66b4de012f8 100644 --- a/spring-bootstrap/src/test/java/org/springframework/bootstrap/context/initializer/ConfigFileApplicationContextInitializerTests.java +++ b/spring-bootstrap/src/test/java/org/springframework/bootstrap/context/initializer/ConfigFileApplicationContextInitializerTests.java @@ -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")); diff --git a/spring-bootstrap/src/test/resources/moreproperties.properties b/spring-bootstrap/src/test/resources/moreproperties.properties new file mode 100644 index 00000000000..f55627b17fd --- /dev/null +++ b/spring-bootstrap/src/test/resources/moreproperties.properties @@ -0,0 +1 @@ +my.property=frommorepropertiesfile