Skip to content

Commit d984cc0

Browse files
author
Phillip Webb
committed
Restore loading order of 'application.properties'
Restore the order that `ConfigFileApplicationListener` attempts to load application.properties so that local files always have a higher precedence than bundled classpath files. This fixes a regression introduced with 1.0.0.RC2. Fixes spring-projectsgh-370
1 parent b69c659 commit d984cc0

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ public class ConfigFileApplicationListener implements
9696

9797
private static final String CONFIG_LOCATION_PROPERTY = "spring.config.location";
9898

99-
private static final String DEFAULT_SEARCH_LOCATIONS = "classpath:/,file:./,"
100-
+ "classpath:/config/,file:./config/";
99+
private static final String DEFAULT_SEARCH_LOCATIONS = "file:./,file:./config/,"
100+
+ "classpath:/,classpath:/config/";
101101

102102
private static final String DEFAULT_NAMES = "application";
103103

@@ -192,6 +192,8 @@ public int getOrder() {
192192

193193
/**
194194
* Set the search locations that will be considered as a comma-separated list.
195+
* Locations are considered in the order specified, with earlier items taking
196+
* precedence.
195197
*/
196198
public void setSearchLocations(String locations) {
197199
Assert.hasLength(locations, "Locations must not be empty");

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@
1616

1717
package org.springframework.boot.context.config;
1818

19+
import java.io.File;
20+
import java.io.FileOutputStream;
21+
import java.io.OutputStream;
1922
import java.lang.reflect.Field;
2023
import java.util.ArrayList;
2124
import java.util.Arrays;
2225
import java.util.Collection;
2326
import java.util.List;
27+
import java.util.Properties;
2428

2529
import org.hamcrest.Description;
2630
import org.hamcrest.Matcher;
@@ -93,6 +97,29 @@ public void loadTwoPropertiesFile() throws Exception {
9397
assertThat(property, equalTo("frompropertiesfile"));
9498
}
9599

100+
@Test
101+
public void localFileTakesPrecedenceOverClasspath() throws Exception {
102+
File localFile = new File(new File("."), "application.properties");
103+
assertThat(localFile.exists(), equalTo(false));
104+
try {
105+
Properties properties = new Properties();
106+
properties.put("my.property", "fromlocalfile");
107+
OutputStream out = new FileOutputStream(localFile);
108+
try {
109+
properties.store(out, "");
110+
}
111+
finally {
112+
out.close();
113+
}
114+
this.initializer.onApplicationEvent(this.event);
115+
String property = this.environment.getProperty("my.property");
116+
assertThat(property, equalTo("fromlocalfile"));
117+
}
118+
finally {
119+
localFile.delete();
120+
}
121+
}
122+
96123
@Test
97124
public void loadTwoOfThreePropertiesFile() throws Exception {
98125
EnvironmentTestUtils.addEnvironment(this.environment, "spring.config.location:"

0 commit comments

Comments
 (0)