Get started Bring yourself up to speed with our introductory content.

How to use Spring Boot file to externalize configuration

Novice programmers rarely see the value in externalized configuration. It's tedious and won't result in an immediate payoff. That means it's a task that is often overlooked or done poorly when it's undertaken at all.

For those who create applications and microservices with Spring Boot, however, the availability of configuration annotations and the file makes externalizing data simple. With Spring Boot configuration tools, externalization is largely straightforward, which means that novice developers no longer have any excuses to eschew the responsibility of configuration-based best practices from the get-go.

A Spring Boot configuration example

To demonstrate how Spring Boot configuration works, we'll start with the creation of a Spring Starter Project with the Spring Tool Suite (STS). To keep things simple, other than the Spring Boot configuration processor to be added later, all Maven dependencies will be defined by STS when the project is created.

The sample project settings are as follows:

  • GroupId: com.mcnz.spring.configuration.example
  • ArtifactId: spring-configuration-example
  • Version: 1.0-SNAPSHOT
  • Name: Spring Configuration Tutorial
    Spring Boot project
    Create a simple Spring Boot project that contains the file

The Spring configuration file

Spring Boot projects store their configuration data in a properties file. By convention, this Spring configuration file is placed in the resources folder of the project and named

It should be noted that this resources folder is simply a design-time artifact that helps to make resource management easier. When a Maven build runs, files in this resources folder are placed into the classes directory of the application alongside all of the compiled Java files.

Spring Boot
Location of Spring Boot file

The Spring file

To demonstrate how the Spring configuration file works, we will fill the file with a variety of data types, including:

  • basic String data;
  • a list of values;
  • a data map; and
  • an inner class with properties.

Here is what the Spring Boot file looks like with our data added to it:

#Spring Boot configuration file example

# simple spring configurationd data = "Dreamix"
spring.boot.config.example.suite = 1113 = true

# list data in spring configuration file
spring.boot.config.example.list[0] = "list-data-0"
spring.boot.config.example.list[1] = "list-data-1"

# simple map in a spring configuration file"value1""value2"

# inner class in Spring config file
spring.boot.config.example.innerClass.property1 = "annotations"
spring.boot.config.example.innerClass.property2 = "xml"
spring.boot.config.example.innerClass.property3 = "YAML"

The Spring @ConfigurationProperties annotation

Next, a user should create a class that will be decorated with the @ConfigurationProperties and @Component Spring annotations to review the properties entered into the Spring Boot configuration file. We will call this class SpringBootConfiguration.

package com.mcnz.spring.configuration;
import org.springframework.stereotype.*;

public class SpringBootConfiguration {


The prefix argument shows which properties, starting with this prefix, will be used by the class decorated with the @ConfigurationProperties annotation.

I'd also recommend for you to add the Spring Boot configuration processor as a dependency to the POM file, which is shown here. If you do not, the software editor will show a warning that indicates this resource should be explicitly referenced in the POM.


Access String data through @ConfigurationProperties

Now, it's time for the cool part.

To access the basic String properties defined in the Spring Boot configuration file, all you need to do is add a property whose name matches one in the file, along with the corresponding setters and getters. The SpringBootConfiguration example class looks like this:

package com.mcnz.spring.configuration;

import org.springframework.stereotype.*;

public class SpringBootConfiguration {
	private String company;
	private int suite;
	private boolean active;
	public String getCompany() { return company; }
	public void setCompany(String c) { company = c; }
	public int getSuite() { return suite; }
	public void setSuite(int s) { suite = s; }
	public boolean isActive() { return active; }
	public void setActive(boolean a) { = a; }

We can even test this in the SpringConfigurationExampleApplication class that was created as part of the Spring framework. All we need to do to quickly test this in the @SpringBootApplication class is:

  • implement the CommandLineRunner interface;
  • override the run() method; and
  • Autowire the SpringBootConfiguration bean into the class.

It looks as follows:

externalized configuration test
Spring externalized configuration test class.

And as you would expect, when the class runs, the value output is as follows:


Convert into a Java List or Map

We can also convert the Map and List data defined in the Spring configuration file into Java objects. You can do this by declaring instances of the appropriate type in the code:

Create inner classes from Spring property files

A user can declare the inner class within the configuration class, annotate it with @ConfigurationProperties and point the prefix attribute to the root name of the properties to be used. The Spring Boot configuration will take care of the rest.

public class SpringBootConfiguration {
	private final InnerClass innerClass = new InnerClass();
	public InnerClass getInnerClass() {
		return innerClass;
	public class InnerClass {
		private String property1;
		private String property2;
		private String property3;
		public String getProperty1() {return property1;}
		public void setProperty1(String p) { property1 = p; }
		public String getProperty2() {return property2;}
		public void setProperty2(String p) { property2 = p; }
		public String getProperty3() {return property3;}
		public void setProperty3(String p) { property3 = p; }

/* Previously coded properties getters and setters omitted */

When saved, the List, Map and InnerClass can all be easily accessed anywhere the SpringConfigurationExampleApplication class is autowired.

Here is the complete SpringConfigurationExampleApplication with the configuration class autowired in:

package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class SpringConfigurationExampleApplication implements CommandLineRunner  {
	SpringBootConfiguration config;

	public static void main(String[] args) {, args);
    public void run(String... args) throws Exception {


When the Spring configuration test class runs, the final output is observed:

Spring boot configuration
Output from running the Spring Boot configuration class application

As an application matures and gets regularly updated, we need a way to easily change the configurations that doesn't require hours to check which property we change and search where it is located in the code. Other benefits of an externalized configuration include:

  • better organization of the project;
  • better reliability;
  • cost reduction and risks;
  • easy refactoring; and
  • flexibility when testing.

Spring Boot's ability to externalize configuration makes it easy to define data in simple property files and subsequently access that data anywhere that the Spring Boot configuration class is autowired into your application.

The source code for this application, developed by Nikolay Stanev at the custom software development company Dreamix, can be found on GitHub.

View All Videos
App Architecture
Software Quality
Cloud Computing