How to read properties file outside jar in Spring Boot

Spring boot can to read properties files from outside jars that contains application configurations in key-value pairs. The externalised properties files are not part of the application jar can be linked dynamically in the command line arguments. The external properties files are saved outside of jar, which is loaded and read when the spring boot application is launched.

Spring boot allows to externalise the properties files from the application jars , which may then be passed as command line arguments. The command line properties are converted into Spring Boot Environment properties by the Spring Boot application. The properties specified in the command line take precedence over those from other sources.



How to read single property in command line

Maven will be used to create executable jars in the spring boot application. The java command is used to launch executable jars. The java command allows you to configure properties in the command line by using the double hyphen symbol. The properties set in the command line take precedence over the properties set in the executable jars.

java -jar target/SpringBootExternalConfiguration-0.0.1-SNAPSHOT.jar --welcome.message="Welcome to Yawin"


How to read properties file outside jar

The external properties file saved outside of the jar can be accessed by specifying the properties file path in the command line argument. The path to the files in the properties might be either absolute or relative. The spring.config.location property allows you to provide the location of the external properties file on the command line.

java -jar -Dspring.config.location=/Users/yawin/custom/extconf.properties target/SpringBootExternalConfiguration-0.0.1-SNAPSHOT.jar

WINDOWS
-------
java -jar -Dspring.config.location=c:\extconf.properties target/SpringBootExternalConfiguration-0.0.1-SNAPSHOT.jar
java -jar -Dspring.config.location=./extconf.properties target/SpringBootExternalConfiguration-0.0.1-SNAPSHOT.jar


How to read properties file from class path

The properties file may be available in several jar files in various folder paths. Property files that are available in the class path cannot be found in the file path. The classpath keyword in the spring.config.location configuration may be used to include external properties files that are available in the class path.

java -jar -Dspring.config.location=classpath:/custom/extconf.properties target/SpringBootExternalConfiguration-0.0.1-SNAPSHOT.jar
java -jar -Dspring.config.location=classpath:/com/yawintutor/properties/application.properties target/SpringBootExternalConfiguration-0.0.1-SNAPSHOT.jar


How to read properties file from environment variables

The system environment variables can be used to configure the properties file. Java can read the path to the properties file from the system environment variable and load it. The SPRING CONFIG LOCATION system environment variable is used to configure the external properties file in the spring boot application.

set SPRING_CONFIG_LOCATION=classpath:/com/yawintutor/properties/application.properties
java -jar target/SpringBootExternalConfiguration-0.0.1-SNAPSHOT.jar
set SPRING_CONFIG_LOCATION=c:\extconf.properties
java -jar target/SpringBootExternalConfiguration-0.0.1-SNAPSHOT.jar

SPRING_CONFIG_LOCATION=~/extconf.properties
java -jar target/SpringBootExternalConfiguration-0.0.1-SNAPSHOT.jar


How to read properties file programmatically

The spring boot application may read external properties files. The SpringApplicationBuilder class is used to read properties files from a remote location. The absolute file path or the class path can be used to provide the location of the properties file. When the spring boot application starts up, it loads the properties files.

package com.yawintutor;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;

@SpringBootApplication
public class SpringBootExternalConfiguration {

	public static void main(String[] args) {
		ConfigurableApplicationContext applicationContext = new SpringApplicationBuilder(SpringBootExternalConfiguration.class)	.properties("spring.config.location:classpath:/com/yawintutor/properties/application.properties").build().run(args);
		ConfigurableEnvironment environment = applicationContext.getEnvironment();
		System.out.println(environment.getProperty("welcome.message"));
	}

}