Skip to content

Commit 889a97a

Browse files
author
Dave Syer
committed
Default spring.config.location to file: if no prefix supplied
Fixes spring-projectsgh-198
1 parent aebaa58 commit 889a97a

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

spring-boot/src/main/java/org/springframework/boot/context/listener/ConfigFileApplicationListener.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,16 @@ private List<String> getCandidateLocations(ResourceLoader resourceLoader) {
263263

264264
private PropertySource<?> load(ConfigurableEnvironment environment,
265265
ResourceLoader resourceLoader, String location, String profile) {
266-
location = environment.resolvePlaceholders(location);
266+
267+
String path = environment.resolvePlaceholders(location);
268+
if (LOCATION_VARIABLE.equals(location) && !path.contains("$")) {
269+
if (!path.contains(":")) {
270+
path = "file:" + path;
271+
}
272+
path = StringUtils.cleanPath(path);
273+
}
274+
location = path;
275+
267276
String suffix = "." + StringUtils.getFilenameExtension(location);
268277
Class<?> type = this.propertySourceAnnotations.configuration(location);
269278

spring-boot/src/test/java/org/springframework/boot/context/listener/ConfigFileApplicationListenerTests.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,8 @@ public void yamlSetsProfiles() throws Exception {
153153

154154
@Test
155155
public void yamlProfileCanBeChanged() throws Exception {
156-
EnvironmentTestUtils
157-
.addEnviroment(this.environment, "spring.profiles.active:prod");
156+
EnvironmentTestUtils.addEnviroment(this.environment,
157+
"spring.profiles.active:prod");
158158
this.initializer.setNames("testsetprofiles");
159159
this.initializer.onApplicationEvent(this.event);
160160
assertThat(Arrays.asList(this.environment.getActiveProfiles()).toString(),
@@ -189,6 +189,25 @@ public void specificResource() throws Exception {
189189
assertThat(this.environment.getProperty("foo"), equalTo("bucket"));
190190
}
191191

192+
@Test
193+
public void specificResourceAsFile() throws Exception {
194+
String location = "file:src/test/resources/specificlocation.properties";
195+
EnvironmentTestUtils.addEnviroment(this.environment, "spring.config.location:"
196+
+ location);
197+
this.initializer.onApplicationEvent(this.event);
198+
assertThat(this.environment.getPropertySources().contains(location), is(true));
199+
}
200+
201+
@Test
202+
public void specificResourceDefaultsToFile() throws Exception {
203+
String location = "src/test/resources/specificlocation.properties";
204+
EnvironmentTestUtils.addEnviroment(this.environment, "spring.config.location:"
205+
+ location);
206+
this.initializer.onApplicationEvent(this.event);
207+
assertThat(this.environment.getPropertySources().contains("file:" + location),
208+
is(true));
209+
}
210+
192211
@Test
193212
public void propertySourceAnnotation() throws Exception {
194213
SpringApplication application = new SpringApplication(WithPropertySource.class);

0 commit comments

Comments
 (0)