Spring Boot Externalized Configuration

Farzin Pashaee
3 min readMar 17, 2022

One of the useful features of Spring Boot is configuration externalization. It helps to have application configuration and setting stored separately so the code can be run in different environments. There are different options to store or apply configuration in spring boot including Java properties files, YAML files, environment variables, and command-line arguments.

Using these properties are pretty easy they can be injected into a managed bean using @value annotation:

@Component
public class ConfigurationLoad {
@Value("${fp.config.sample}")
private String sample;
...

or be bound to structured objects through @ConfigurationProperties:

@Configuration 
@ConfigurationProperties(prefix = "fp.config")
public class ConfigProperties {
private String sample;
private List<String> sampleArray;
// getters and setters
...

Java Application Properties

Basically using the Spring Boot Initializer you will get the default application properties as a tool to externalize configuration. By default, you can find this file inside the src/main/resources directory.

Spring Boot will automatically find and load application.properties and application.yaml files from the following locations when your application starts:

  1. From the classpath
    – The classpath root
    – The classpath /config package
  2. From the current directory
    – The current directory
    – The /config the subdirectory in the current directory
    – Immediate child directories of the /config subdirectory

There are options to change the location and the name of the properties file which you can find the commands in the Spring Boot reference page.

Inside this file, you can have your customized and also spring-related configurations as follow:

fp.config.sample=sampleValue
fp.config.sampleArray=v1,v2,v3

The variables inside the application properties file can have a single value or multiple values as an array. In addition to that, properties can be set up for different profiles and also can locate outside the final jar file. This capability gives the developer the option to run applications in multiple environments by having multiple files for different profiles. The files naming by default will be like this:

Configuration related for each profile will be loaded and it will override application.properties values as the application start with the proper profile.

Config data files order for the mentioned files are as follow

  1. Application properties packaged inside your jar
  2. Profile-specific application properties packaged inside your jar
  3. Application properties outside of your packaged jar
  4. Profile-specific application properties outside of your packaged jar

Another useful feature is configuring random values inside the properties file which can be done with proper placeholders.

fp.config.sample=${random.value} 
fp.config.sampleNumber=${random.int}

YAML

The YAML is very similar to the application properties file by nature and only different inside the file itself. for example, the properties file represented in the previous section will look like this:

fp:
config:
sample: "sampleValue"
sampleArray:
- "v1"
- "v2"
- "v3"

OS Environment Variable

Spring Boot also gives you the option to use os environment variables for externalizing application properties. These variables can be easily used inside properties files or directly injected into a managed bean.

# Windows machine
C:\Users\fp\projects\springboot>echo %SYSTEM_VAR%
# "EnvironmentVariable" will print

Using @value You can inject the value of the environment variable into your bean as follow:

@Component
public class ConfigurationLoad {
@Value("${SYSTEM_VAR}")
private String sample; // EnvironmentVariable will be set
...

Or can use the properties file to set environment variable into a field:

fp.config.systemVar=${SYSTEM_VAR}

Command Line Properties

Another way to override and add new properties to the Spring Boot application is to use command-line properties. To do so you can use any of the following approaches.

java -jar -Dfp.config.sample=sampleValueChanged application.jar
# or
java -jar
application.jar --fp.config.sample=sampleValueChanged

These values on the command line will override the properties files values.

--

--

Farzin Pashaee
Farzin Pashaee

Written by Farzin Pashaee

Software Engineer at Maybank, AI and ML enthusiastic

Responses (1)