Reading application.properties in Spring Boot

With this short blog post, I am going to share with you a few ways you can read application properties from application.properties file in Spring Boot. I am going to share 3 ways:

  • 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 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 created in src/main/resources folder. If for some reason you do not have application.properties file in src/main/resources folder of your Spring Boot project you can create this file manually.

Read application.properties Using the Environment

Most probably one of the easiest ways to read a property from application.properties file is by autowiring an Environment object. All you need to do is:

1) Use the @Autowired annotation to inject the Environment object into your Rest Controller or Service class, like so:

@Autowired
private Environment env;

2) User getProperty(String key) method to get a value for a specific property. Like this:

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 property key name as request parameter and returns 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 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 value of a requested property.

Reading Properties with @Value Annotation

Another very simple way to read application properties is to use @Value annotation. Simply annotation the class field with @Value annotation providing the name of the property you want to read from application.properties file and class field variable will be assigned that value.

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 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 I needed to do.

Reading Application Properties with @ConfigurationProperties

Another way to read application properties in Spring Boot app 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 a prefix of app we will need to annotation our Java Bean with:

@ConfigurationProperties("app")

Here is an example of 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 autowire it by 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! Three very simple ways to read application properties in you Spring Boot app.

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