From 889a97a6b21bec1800fe608bf15e6c64a91e1bce Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Thu, 9 Jan 2014 12:22:42 +0000 Subject: [PATCH] Default spring.config.location to file: if no prefix supplied Fixes gh-198 --- .../ConfigFileApplicationListener.java | 11 ++++++++- .../ConfigFileApplicationListenerTests.java | 23 +++++++++++++++++-- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/spring-boot/src/main/java/org/springframework/boot/context/listener/ConfigFileApplicationListener.java b/spring-boot/src/main/java/org/springframework/boot/context/listener/ConfigFileApplicationListener.java index 0c03a62782..d762b6a95d 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/listener/ConfigFileApplicationListener.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/listener/ConfigFileApplicationListener.java @@ -263,7 +263,16 @@ public class ConfigFileApplicationListener implements private PropertySource load(ConfigurableEnvironment environment, ResourceLoader resourceLoader, String location, String profile) { - location = environment.resolvePlaceholders(location); + + String path = environment.resolvePlaceholders(location); + if (LOCATION_VARIABLE.equals(location) && !path.contains("$")) { + if (!path.contains(":")) { + path = "file:" + path; + } + path = StringUtils.cleanPath(path); + } + location = path; + String suffix = "." + StringUtils.getFilenameExtension(location); Class type = this.propertySourceAnnotations.configuration(location); diff --git a/spring-boot/src/test/java/org/springframework/boot/context/listener/ConfigFileApplicationListenerTests.java b/spring-boot/src/test/java/org/springframework/boot/context/listener/ConfigFileApplicationListenerTests.java index 4a70be5b2f..ce7a6c6307 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/listener/ConfigFileApplicationListenerTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/listener/ConfigFileApplicationListenerTests.java @@ -153,8 +153,8 @@ public class ConfigFileApplicationListenerTests { @Test public void yamlProfileCanBeChanged() throws Exception { - EnvironmentTestUtils - .addEnviroment(this.environment, "spring.profiles.active:prod"); + EnvironmentTestUtils.addEnviroment(this.environment, + "spring.profiles.active:prod"); this.initializer.setNames("testsetprofiles"); this.initializer.onApplicationEvent(this.event); assertThat(Arrays.asList(this.environment.getActiveProfiles()).toString(), @@ -189,6 +189,25 @@ public class ConfigFileApplicationListenerTests { assertThat(this.environment.getProperty("foo"), equalTo("bucket")); } + @Test + public void specificResourceAsFile() throws Exception { + String location = "file:src/test/resources/specificlocation.properties"; + EnvironmentTestUtils.addEnviroment(this.environment, "spring.config.location:" + + location); + this.initializer.onApplicationEvent(this.event); + assertThat(this.environment.getPropertySources().contains(location), is(true)); + } + + @Test + public void specificResourceDefaultsToFile() throws Exception { + String location = "src/test/resources/specificlocation.properties"; + EnvironmentTestUtils.addEnviroment(this.environment, "spring.config.location:" + + location); + this.initializer.onApplicationEvent(this.event); + assertThat(this.environment.getPropertySources().contains("file:" + location), + is(true)); + } + @Test public void propertySourceAnnotation() throws Exception { SpringApplication application = new SpringApplication(WithPropertySource.class);