Application Configuration with Spring Boot

blog details
author Ranga Karanam July 05, 2022 5 minutes

This guide will introduce you to the various application configuration options that are present in Spring Boot. You will also learn how to create custom configuration for an application.


You will learn

  • What is application configuration?
  • Why do we need application configuration?
  • What is
  • What are some of the important things that can customized in
  • How do you externalize configuration using Spring Boot?
  • How can you add custom application properties using Spring Boot?
  • What is @ConfigurationProperties?
  • What are the advantages of using @ConfigurationProperties?

Need for Application Configuration

Configuration for applications vary from one environment to another

  • You would want to connect to a different database or queues
  • You would want to connect with different services
  • You would want to configure less logging in production
  • You might want to have different custom configuration

Application Configuration with Spring Boot

Spring Boot allows you to configure your application configuration using a file named can reside anywhere in the classpath of the application.

In, we can use the “logging.level” prefix to set logging levels.


You can configure a log file by using logging.file property in The logging here would be in addition to the logging in console.


You can also configure the port to run the server on using server.port

server.port = 9080

For complete list of options refer spring documentation -

In this guide, we will create three services using proper URIs and HTTP methods:

Project Code Structure

Following screenshot shows the structure of the project we will create.


A few details:

  • - The Spring Boot Application class generated with Spring Initializer. This class acts as the launching point for application.
  • pom.xml - Contains all the dependencies needed to build this project.
  • - We will use this component to define application configuration using @ConfigurationProperties
  • - Example of a resource using @Value
  • - Configuration for application

Tools you will need

  • Maven 3.0+ is your build tool
  • Your favorite IDE. We use Eclipse.
  • JDK 1.8+

Complete Maven Project With Code Examples

Our Github repository has all the code examples -

Bootstrapping with Spring Initializr

Spring Initializr is great tool to bootstrap your Spring Boot projects.



As shown in the image above, following steps have to be done

  • Launch Spring Initializr and choose the following
    • Choose com.in28minutes.springboot.tutorial.basics.application.configuration as Group
    • Choose spring-boot-tutorial-basics-configuration as Artifact
    • Choose following dependencies
      • Web
      • DevTools
  • Click Generate Project.
  • Import the project into Eclipse. File -> Import -> Existing Maven Project.

Application Configuration using @Value

Let’s create a very simple example.

public class WelcomeResource {

	private String welcomeMessage;

	public String retrieveWelcomeMessage() {
		// Complex Method
		return welcomeMessage;


  • @Value("${welcome.message}") private String welcomeMessage; - Pick up the value for welcome.message from application configuration and assign it to this member variable.
  • @GetMapping("/welcome") public String retrieveWelcomeMessage() - Expose a simple service using the configuration

Let’s configure a value for welcome message in the

welcome.message=Welcome message from property file! Welcome to in28Minutes

When you launch http://localhost:8080/welcome, you would see a page with this message

Welcome message from property file! Welcome to in28Minutes

We can make the more creative by using other properties as variables too.
welcome.message=Welcome message from property file! Welcome to ${}

Spring Boot provides different ways of configuring your property values.

  • You can send in a program argument
    • –welcome.message=”SomethingElse”
  • You can use a different file as property file
    • –spring.config.location=classpath:/
  • You can configure an environment variable

@Value is the basic approach for application configuration using Spring.

Application Configuration using Type-safe Configuration Properties

The problem with @Value is that you would have your configuration values distributed through out your application. A better option would be to have a centralized approach.

You can define a configuration component using @ConfigurationProperties.

public class BasicConfiguration {
    private boolean value;
    private String message;
    private int number;

    public boolean isValue() {
        return value;

    public void setValue(boolean value) {
        this.value = value;

    public String getMessage() {
        return message;

    public void setMessage(String message) {
        this.message = message;

    public int getNumber() {
        return number;

    public void setNumber(int number) {
        this.number = number;



  • @Component @ConfigurationProperties("basic") - Defines that this java bean contains configuration properties. All property names will start with basic..
  • private boolean value - A boolean value. Can be configured as basic.value.
  • private String message - A String value. Can be configured as basic.message.

We would need to use the BasicConfiguration in a service to expose the values

private BasicConfiguration configuration;

public Map dynamicConfiguration() {
    // Not the best practice to use a map to store differnt types!
    Map map = new HashMap();
    map.put("message", configuration.getMessage());
    map.put("number", configuration.getNumber());
    map.put("key", configuration.isValue());
    return map;


  • @Autowired private BasicConfiguration configuration - Its very easy to use BasicConfiguration. Autowire it in when ever you need the value for a property from it.
  • @RequestMapping("/dynamic-configuration") public Map dynamicConfiguration() { - Define a simple service to expose the configured values.

The values can be configured in

basic.value: true
basic.message: Dynamic Message
basic.number: 100

When you browse to http://localhost:8080/dynamic-configuration, you see the following response.

{"number":100,"message":"Dynamic Message","key":true}

Understand Type Safety

@ConfigurationProperties is type safe. If you configure any property with a value of an invalid type, the application fails at startup.

Below snippet shows the log when basic.number is assigned a non numberic value.



Binding to target com.in28minutes.springboot.configuration.BasicConfiguration@391b8545 failed:

    Property: basic.number
    Value: ABC
    Reason: Failed to convert property value of type [java.lang.String] to required type [int] for property 'number'; nested exception is org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.String] to type [int]


Update your application's configuration

Complete Code Example

Github repository has all the code examples -

Just Released