Different key value formats in @Value and application.properties in spring boot

In this post, we’ll see the different key formats used in the @Value annotation and the spring boot application.properties file. Spring boot helps you to customize keys and values in a variety of ways. At startup, the spring boot extracts the respective value and assigns it to the Java variable.

Default String Value to a variable

The @Value annotation assigns the default value of the string to the java variable. The string should be added to the @Value annotation as shown in the example below. 

package com.yawintutor;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class TestController {

    @Value("message")
    private String message;
    
    @GetMapping("/")
    public String welcome(Model model) {
    	System.out.println("Message : "+message);
        return "welcome";
    }
}

Output

Message : message

Read from application.properties file

The @Value annotation allows to read a property from the application.properties file by using ${…} format. The property key is added as a string to the @Value annotation. When starting the spring boot application, check the application.properties key and assign the value to the Java variable.

application.properties

message=Yawin Tutor
package com.yawintutor;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class TestController {

    @Value("${message}")
    private String message;
    
    @GetMapping("/")
    public String welcome(Model model) {
    	System.out.println("Message : "+message);
        return "welcome";
    }
}

Output

Message : Yawin Tutor

Nested format in application.properties

The value of the key in the application.properties may have another key placeholder. In this case, the spring boot will find the value of the key and substitute it with the actual key. The example below illustrates how to use a nested placeholder key format.

application.properties
------------------------
message=Yawin Tutor
welcome.message=Welcome to ${message}

Java
----
    @Value("${welcome.message}")
    private String message;

Output
------
Message : Welcome to Yawin Tutor

The below example shows how to use dynamic key with in another property value.

application.properties
------------------------
message=Yawin Tutor
key=message
welcome.message=Welcome to ${${key}}

Java
----
    @Value("${welcome.message}")
    private String message;

Output
------
Message : Welcome to Yawin Tutor

Nested format in @Value annotation

The nested key format can be used in @Value annotation. At startup, spring boot application checks the inner key placeholder and find the key for the outer placeholder. In the below example ${key} return with a String “message”. The outer key placeholder is constructed as ${message} and find the value for key “message”.

application.properties
------------------------
message=Yawin Tutor
key=message

Java
----
    @Value("Welcome to ${${key}}")
    private String message;

Output
------
Message : Welcome to Yawin Tutor

Default value in application.properties

The default value is set in key place holder using “:” colon after the key name. The below example shows how to use the default value.

application.properties
------------------------
message=Yawin Tutor
welcome.message=Welcome to ${msg:Tutor World}

Java
----
    @Value("${welcome.message}")
    private String message;

Output
------
Message : Welcome to Tutor World

Default value using @Value annotation

The example below shows how to use default value in the @Value annotation. The default value should be added after the key name with “:” colon as separator.

application.properties
------------------------
message=Yawin Tutor

Java
----
    @Value("${welcome.message:Welcome to Tutor World}")
    private String message;

Output
------
Message : Welcome to Tutor World
application.properties
------------------------
message=Yawin Tutor

Java
----
    @Value("${welcome.message:Welcome to ${message}}")
    private String message;

Output
------
Message : Welcome to Yawin Tutor



Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *