Reading application.properties in Spring Boot

In this blog post, you will learn about three different ways to read application properties in the Spring Boot application.

  • Read application.properties using Environment object, 
  • Read a property from application.properties file using @ConfigurationProperties
  • and reading a property using the @Value annotation.

To demonstrate how to read application properties in the Spring Boot application, I have created a very simple Spring Boot Web App. You can follow this tutorial, “Create a Simple Web Service Project with Spring Boot” to learn how to create a very simple Web App with Spring Boot.

When your Spring Boot project is created, you should have an application.properties file in src/main/resources folder. If, for some reason, you do not have the application.properties file in the src/main/resources folder of your Spring Boot project, you can create this file manually.

Read properties Using the Environment Object

One of the easiest ways to read a property from the application.properties file is by autowiring an Environment object. All you need to do is to use the @Autowired annotation. It is called dependency injection.

To inject the Environment object using field-based dependency injection, follow these steps:

1) Use the @Autowired annotation to inject the Environment object into your Rest Controller or Service class.

@Autowired
private Environment env;

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=Learning Spring Boot 
app.description=Working with properties file

and I need to create a Web Service endpoint that accepts the property key name as a request parameter and returns the property value.

Here is my Rest Controller class

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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("app")
public class AppController {
 
 @Autowired
 private Environment env;
 
 @GetMapping("/property")
 public String getPropertyValue(@RequestParam("key") String key)
 {
  String returnValue = "No value";
  
  String keyValue = env.getProperty(key);
  
  if( keyValue!= null && !keyValue.isEmpty())
  {
   returnValue = keyValue;
  }
  return returnValue;
 }
}

Please note that to be able to read a property from the application.properties file, I needed to use the @Autowired annotation to inject the Environment object. Then I 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")

    
 @Value("${app.title}")
 private String appTitle;
 
 @GetMapping("/value")
 public String getValue()
 {
  return appTitle;
 }

}

And that’s all you need to do.

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=Learning Spring Boot 
app.description=Working with 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.

I hope this short tutorial was of some value to you. If you are interested in learning more about Spring Boot, check the video courses below, which might help speed up your learning progress. Learning Spring Boot by watching step-by-step video lessons is a very easy and enjoyable way of learning.


1 Comment on "Reading application.properties in Spring Boot"


  1. When I read the properties file from src/main/resources/env.properties
    hostName:1.1.1.1
    userName:test
    Password:test

    In Controller
    @value(${userName})
    Private String userName;

    Instead of test it’s is loading system logged in userName.Could someone help on this to learn in right way.
    But on the other hand when I update the key in env.properties as name as well as in controller it’s picking the right value

    Reply

Leave a Reply to Rathishkumar Cancel reply

Your email address will not be published.