How to read multiple external properties file in Spring Boot

Spring boot can read multiple external properties files via command line arguments that can be either a file path or a class path. The numerous external properties files might be found in a single directory or in multiple locations. The spring boot application configures multiple external properties files using the command line parameters and spring.config.location.

In the spring boot application, command line parameters are converted to system environment variables. The two keywords and spring.config.location can be used to specify multiple external properties files. The specifies the name of the properties file. The location of the properties file is specified by spring.config.location.

In a real-world scenario, many properties files are used in the spring boot application. Application configurations are stored in the properties file as a key-value pair. Because it is a text file that is loaded dynamically from the application, the configuration settings may be changed without recompiling the application. As a result, the spring boot application can read various properties files that are placed outside of the application executable jar.

Reading single external properties file

The spring.config.location property may be used to configure the single external properties file. The file path for the properties might be absolute or relative. Alternatively, the class path can be used to provide the file location for the properties. The classpath keyword is prefixed in the path to define the file’s class path.

When the spring boot application starts, the properties file from the external configuration is loaded and all the properties in the properties file are read.

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

Reading Multiple external properties file from classpath

Using command line options, Spring Boot lets you to load multiple properties files from outside the jar. Two keywords are used to load multiple external properties files from outside the jar. The and spring.config.location keywords are used to define the file name and location of the properties.

In the following example, two properties files are placed in two distinct classpath locations.

java -jar,myconf -Dspring.config.location=classpath:/custom1/,classpath:/com/yawintutor/properties target/SpringBootExternalConfiguration-0.0.1-SNAPSHOT.jar

Reading Multiple external properties file

Multiple external properties files can be read from various file locations in the file path. The absolute or relative file path can be provided. Property files will be loaded from a number of external file locations.

java -jar,myconf -Dspring.config.location=~/custom1/,/user/yawin/custom2/ target/SpringBootExternalConfiguration-0.0.1-SNAPSHOT.jar

Reading multiple external properties file from environment variables

The system environment variable can be used to configure the multiple external properties files. The java command will use the environment variable to locate the external properties file and load the properties from the properties file. The system environment variable is used to separate the configuration from the environment’s execution.

set SPRING_CONFIG_NAME=extconf,myconf
set SPRING_CONFIG_LOCATION=~/custom1/,/user/yawin/custom2/
java -jar target/SpringBootExternalConfiguration-0.0.1-SNAPSHOT.jar

Read multiple external properties file programmatically

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

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;

public class SpringBootExternalConfiguration {

	public static void main(String[] args) {
		ConfigurableApplicationContext applicationContext = new SpringApplicationBuilder(SpringBootExternalConfiguration.class)	.properties(",myconf","spring.config.location:classpath:~/custom1/,/user/yawin/custom2/").build().run(args);
		ConfigurableEnvironment environment = applicationContext.getEnvironment();