Roufid

Load multiple configuration files from different directories with Spring Boot

Spring Boot

This tutorial will show how to load multiple configuration files from different directories with Spring Boot.

By default, Spring Boot look for your externalized configuration file ( application.proroperties ) in four predetermined locations :

You may want to load multiple externalized configuration files located in different directories. This tutorial will show how to do it.

Example

Consider the following project structure :

Spring Boot multiple configuration files

application.properties and conf.properties are two configuration files we want to load in our Spring Boot application.

Command line arguments

The first way to tell Spring Boot to load our configuration files is by using command arguments. Spring Boot provides the argument  spring.config.name to set configuration files names seperated with a comma. The second command line argument is  spring.config.location in which you must set the locations where Spring Boot will find your externalized configuration files. 

Below the usage in our situation :

Spring Boot will now pick up application.properties from /com/roufid/tutorial/configuration/ and conf.properties from /external/properties/.

Environment variables

You can use environment variables to tell Spring Boot to pick up what you want where you want. set the name of your externalized configuration files in the SPRING_CONFIG_NAME environment variable seperated with a comma and the locations in SPRING_CONFIG_LOCATION.

Below an example :

Programmatically

You can programmatically tell Spring Boot to load your configuration files from custom location as below :

References