In a Spring Boot application, the application.properties or application.yml file is a central place which contains all configurations or settings of the application, in form of key-value pairs. Let's learn how to read or bind values of properties in Spring application using Java code. In this blog post, you will learn about three different ways to read application properties values in the Spring Boot application.
1. Using Environment object
2. Using @ConfigurationProperties
3. Using the @Value annotation
When ever you create Spring Boot project, you should see an application.properties or application.yml file in src/main/resources folder. If, for some reason, you do not have the application.properties or application.yml file in the src/main/resources folder of your Spring Boot project, you can create this file manually.
Read properties Using the Environment Object
In order to inject the Environment object using field-based dependency injection, follow these steps:
Step 1) Use the @Autowired annotation to inject the Environment object into your Rest Controller or Service class.
@Autowired
private Environment env;
Step 2) Use the getProperty(String key) method to get a value for a specific property.
String keyValue = env.getProperty(key);
Let’s assume I have these properties in my application.properties file:
app.title=Spring Boot App
app.description=Read value from properties file
Here is example -
package com.appsdeveloperblog.properties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("app")
public class AppController {
@Autowired
private Environment env;
@GetMapping("/property")
public String getPropertyValue()
{
String returnValue = "";
String keyValue = env.getProperty("app.title");
if( keyValue!= null && !keyValue.isEmpty())
{
returnValue = keyValue;
}
return returnValue;
}
}
Note:- to read a property from the application.properties or application.yml file, you should use the @Autowired annotation to inject the Environment object. Then you can simply call its getProperty(String key) method to get the requested property’s value.
Read Properties using the @Value Annotation
Another very simple way to read application properties is to use @Value annotation.
To read a value from a property file and assign it to a member variable, annotate it with @Value annotation. In brackets, provide the name of the property that you want to read. For example, to read the value of app.title property, annotate the class field with @Value annotation like so:
@Value("${app.title}")
private String appTitle;
Let’s assume I have the following application.properties file:
app.title=Learning Spring Boot
app.description=Working with properties file
Here is an example of a Rest Controller that uses @Value annotation to read the app.title property.
package com.appsdeveloperblog.properties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("app")
public class AppController {
@Value("${app.title}")
private String appTitle;
@GetMapping("/value")
public String getValue()
{
return appTitle;
}
}
Read Properties using the @ConfigurationProperties Annotation
Another way to read application properties in the Spring Boot application is to use the @ConfigurationProperties annotation. To do that, we will need to create a Plain Old Java Object where each class field matches the name of the key in a property file.
For example, let’s assume we have the same application.properties file:
app.title=Spring Boot
app.description=read value from properties file
Because each of the property names starts with the prefix “app“, we will need to annotate our Java Bean with @ConfigurationProperties(“app”).
Below is an example of a Java class annotated with @ConfigurationProperties annotation:
package com.appsdeveloperblog.properties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties("app")
public class AppProperties {
private String title;
private String description;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
To use this class in Rest Controller or Service class, we simply inject its object using the @Autowired annotation.
package com.appsdeveloperblog.properties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("app")
public class AppController {
@Autowired
AppProperties myAppProperties;
@GetMapping("/title")
public String getAppTitle()
{
return myAppProperties.getTitle();
}
@GetMapping("/description")
public String getAppDescription()
{
return myAppProperties.getDescription();
}
}
Once we have an instance of our AppProperties class, we can use getters to get the value of the property stored in application.properties file.
And this is it! These are the three very simple ways to read application properties in your Spring Boot app.
0 Comments