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

How to use Spring Boot application.properties 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 application.properties 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 application.properties 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 application.properties.

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 application.properties
Location of Spring Boot application.properties file

The Spring application.properties file

To demonstrate how the application.properties 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 application.properties file looks like with our data added to it:

#Spring Boot configuration file example

# simple spring configurationd data
spring.boot.config.example.company = "Dreamix"
spring.boot.config.example.suite = 1113
spring.boot.config.example.active = 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
spring.boot.config.example.map.key1="value1"
spring.boot.config.example.map.key2="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.boot.context.properties.*;
import org.springframework.stereotype.*;

@ConfigurationProperties(prefix="spring.boot.config.example")
@Component
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.

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-configuration-processor</artifactId>
     <optional>true</optional>
</dependency>

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 application.properties file, along with the corresponding setters and getters. The SpringBootConfiguration example class looks like this:

package com.mcnz.spring.configuration;

import org.springframework.boot.context.properties.*;
import org.springframework.stereotype.*;

@ConfigurationProperties(prefix="spring.boot.config.example")
@Component
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) { this.active = 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:

"Dreamix"
1113
true

Convert application.properties 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.

@ConfigurationProperties(prefix="spring.boot.config.example")
@Component
public class SpringBootConfiguration {
	
	private final InnerClass innerClass = new InnerClass();
	
	public InnerClass getInnerClass() {
		return innerClass;
	}
	
	@ConfigurationProperties(prefix="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;

@SpringBootApplication
public class SpringConfigurationExampleApplication implements CommandLineRunner  {
	
	@Autowired
	SpringBootConfiguration config;

	public static void main(String[] args) {
		SpringApplication.run(SpringConfigurationExampleApplication.class, args);
	}
	
    @Override
    public void run(String... args) throws Exception {
        System.out.println(config.getCompany());
        System.out.println(config.getSuite());
        System.out.println(config.isActive());
        
        System.out.println(config.getList().get(0));
        System.out.println(config.getMap());
        System.out.println(config.getInnerClass().getProperty1());
    }

}

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

Join the conversation

2 comments

Send me notifications when other members comment.

Please create a username to comment.

What strategies do you use to enforce coding standards such as always externalizing application properties?
Cancel
Thank you Nikolay. Great article 
Cancel

-ADS BY GOOGLE

SearchCloudApplications

SearchSoftwareQuality

SearchHRSoftware

SearchSAP

SearchERP

DevOpsAgenda

Close