diff --git a/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/PropertiesLauncher.java b/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/PropertiesLauncher.java
index b7693a7b069..ab1a6988326 100644
--- a/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/PropertiesLauncher.java
+++ b/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/PropertiesLauncher.java
@@ -132,17 +132,12 @@ public class PropertiesLauncher extends Launcher {
}
private void initializeProperties(File home) throws Exception, IOException {
- String config = SystemPropertyUtils.resolvePlaceholders(System.getProperty(
- CONFIG_NAME, "application")) + ".properties";
- InputStream resource = getClasspathResource(config);
- if (resource == null) {
- resource = getResource(new File(home, config).getAbsolutePath());
- }
- if (resource == null) {
- config = SystemPropertyUtils.resolvePlaceholders(System.getProperty(
- CONFIG_LOCATION, config));
- resource = getResource(config);
- }
+ String config = "classpath:"
+ + SystemPropertyUtils.resolvePlaceholders(SystemPropertyUtils
+ .getProperty(CONFIG_NAME, "application")) + ".properties";
+ config = SystemPropertyUtils.resolvePlaceholders(SystemPropertyUtils.getProperty(
+ CONFIG_LOCATION, config));
+ InputStream resource = getResource(config);
if (resource != null) {
this.logger.info("Found: " + config);
diff --git a/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/util/SystemPropertyUtils.java b/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/util/SystemPropertyUtils.java
index 058ef76b9c9..df626a72273 100644
--- a/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/util/SystemPropertyUtils.java
+++ b/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/util/SystemPropertyUtils.java
@@ -147,21 +147,55 @@ public abstract class SystemPropertyUtils {
private static String resolvePlaceholder(Properties properties, String text,
String placeholderName) {
+ String propVal = getProperty(placeholderName, null, text);
+ if (propVal != null) {
+ return propVal;
+ }
+ return properties == null ? null : properties.getProperty(placeholderName);
+ }
+
+ public static String getProperty(String key) {
+ return getProperty(key, null, "");
+ }
+
+ public static String getProperty(String key, String defaultValue) {
+ return getProperty(key, defaultValue, "");
+ }
+
+ /**
+ * Search the System properties and environment variables for a value with the
+ * provided key. Environment variables in UPPER_CASE
style are allowed
+ * where System properties would normally be lower.case
.
+ *
+ * @param key the key to resolve
+ * @param text optional extra context for an error message if the key resolution fails
+ * (e.g. if System properties are not accessible)
+ * @return a static property value or null of not found
+ */
+ public static String getProperty(String key, String defaultValue, String text) {
try {
- String propVal = System.getProperty(placeholderName);
+ String propVal = System.getProperty(key);
if (propVal == null) {
// Fall back to searching the system environment.
- propVal = System.getenv(placeholderName);
+ propVal = System.getenv(key);
+ }
+ if (propVal == null) {
+ // Try with underscores.
+ propVal = System.getenv(key.replace(".", "_"));
+ }
+ if (propVal == null) {
+ // Try uppercase with underscores as well.
+ propVal = System.getenv(key.toUpperCase().replace(".", "_"));
}
if (propVal != null) {
return propVal;
}
}
catch (Throwable ex) {
- System.err.println("Could not resolve placeholder '" + placeholderName
- + "' in [" + text + "] as system property: " + ex);
+ System.err.println("Could not resolve key '" + key + "' in '" + text
+ + "' as system property or in environment: " + ex);
}
- return properties == null ? null : properties.getProperty(placeholderName);
+ return defaultValue;
}
private static int findPlaceholderEndIndex(CharSequence buf, int startIndex) {
diff --git a/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/PropertiesLauncherTests.java b/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/PropertiesLauncherTests.java
index fdef6de34db..d3f68360f17 100644
--- a/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/PropertiesLauncherTests.java
+++ b/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/PropertiesLauncherTests.java
@@ -72,6 +72,15 @@ public class PropertiesLauncherTests {
assertEquals("[etc/]", ReflectionTestUtils.getField(launcher, "paths").toString());
}
+ @Test
+ public void testUserSpecifiedConfigPathWins() throws Exception {
+
+ System.setProperty("loader.config.name", "foo");
+ System.setProperty("loader.config.location", "classpath:bar.properties");
+ PropertiesLauncher launcher = new PropertiesLauncher();
+ assertEquals("my.BarApplication", launcher.getMainClass());
+ }
+
@Test
public void testSystemPropertySpecifiedMain() throws Exception {
System.setProperty("loader.main", "foo.Bar");
diff --git a/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/util/SystemPropertyUtilsTests.java b/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/util/SystemPropertyUtilsTests.java
index ce57cb48089..96355eeed6c 100644
--- a/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/util/SystemPropertyUtilsTests.java
+++ b/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/util/SystemPropertyUtilsTests.java
@@ -19,7 +19,6 @@ package org.springframework.boot.loader.util;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
-import org.springframework.boot.loader.util.SystemPropertyUtils;
import static org.junit.Assert.assertEquals;
@@ -53,4 +52,9 @@ public class SystemPropertyUtilsTests {
assertEquals("foo", SystemPropertyUtils.resolvePlaceholders("${bar:${spam:foo}}"));
}
+ @Test
+ public void testEnvVar() {
+ assertEquals(System.getenv("LANG"), SystemPropertyUtils.getProperty("lang"));
+ }
+
}
diff --git a/spring-boot-tools/spring-boot-loader/src/test/resources/bar.properties b/spring-boot-tools/spring-boot-loader/src/test/resources/bar.properties
new file mode 100644
index 00000000000..8301c2649f3
--- /dev/null
+++ b/spring-boot-tools/spring-boot-loader/src/test/resources/bar.properties
@@ -0,0 +1 @@
+loader.main: my.BarApplication