From d984cc08263f41513bc61aac0de8988fd4210e84 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Tue, 18 Feb 2014 23:13:09 -0800 Subject: [PATCH] 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 gh-370 --- .../config/ConfigFileApplicationListener.java | 6 +++-- .../ConfigFileApplicationListenerTests.java | 27 +++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java b/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java index d19472bd01..2be9c9d79b 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java @@ -96,8 +96,8 @@ public class ConfigFileApplicationListener implements private static final String CONFIG_LOCATION_PROPERTY = "spring.config.location"; - private static final String DEFAULT_SEARCH_LOCATIONS = "classpath:/,file:./," - + "classpath:/config/,file:./config/"; + private static final String DEFAULT_SEARCH_LOCATIONS = "file:./,file:./config/," + + "classpath:/,classpath:/config/"; private static final String DEFAULT_NAMES = "application"; @@ -192,6 +192,8 @@ public class ConfigFileApplicationListener implements /** * Set the search locations that will be considered as a comma-separated list. + * Locations are considered in the order specified, with earlier items taking + * precedence. */ public void setSearchLocations(String locations) { Assert.hasLength(locations, "Locations must not be empty"); diff --git a/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java b/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java index e19da0dc8f..96293909d3 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerTests.java @@ -16,11 +16,15 @@ package org.springframework.boot.context.config; +import java.io.File; +import java.io.FileOutputStream; +import java.io.OutputStream; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.List; +import java.util.Properties; import org.hamcrest.Description; import org.hamcrest.Matcher; @@ -93,6 +97,29 @@ public class ConfigFileApplicationListenerTests { assertThat(property, equalTo("frompropertiesfile")); } + @Test + public void localFileTakesPrecedenceOverClasspath() throws Exception { + File localFile = new File(new File("."), "application.properties"); + assertThat(localFile.exists(), equalTo(false)); + try { + Properties properties = new Properties(); + properties.put("my.property", "fromlocalfile"); + OutputStream out = new FileOutputStream(localFile); + try { + properties.store(out, ""); + } + finally { + out.close(); + } + this.initializer.onApplicationEvent(this.event); + String property = this.environment.getProperty("my.property"); + assertThat(property, equalTo("fromlocalfile")); + } + finally { + localFile.delete(); + } + } + @Test public void loadTwoOfThreePropertiesFile() throws Exception { EnvironmentTestUtils.addEnvironment(this.environment, "spring.config.location:"