Spring Boot Externalized Configuration

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:

or be bound to structured objects through @ConfigurationProperties:

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:

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.

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:

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.

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

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

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.

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

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store