How to read the application.properties in spring boot

In this tutorial, we will discuss the major 3 ways by which we can read application.properties in Spring boot.

Let’s discuss what is the use of application.properties in Spring boot.

In the Spring boot framework, we have a built-in mechanism for configuring our application which is done using application.properties. This file is located in the /src/main/resources folder. It provides almost all kinds of configuration properties which help in the smooth working of our spring boot application. For eg. Changing the server port of a web app, database URL configuration, multipart configuration, etc.

The Various ways by which we can read an application.properties file are:

  1. By using the Environment object
  2. By using @ConfigurationProperties
  3. By using @Value annotation

By using the Environment object

  • This is one of the easiest ways to read an application.properties file by just auto-wiring the Environment object. The autowiring process helps to inject the Environment object inside the Rest controller class.
  • With the help of the getProperty function, we can easily get the value of the specific property which was declared in the application.properties file.

content of application.properties file

server.port=9292
app.title=Learning Spring Boot
app.description=Working with a properties file

Code for reading the application.properties file

Enlightenerpackage com.task2.codespeedy.springbootcodespeedy.controller;

import org.spring framework.beans.factory.annotation.Autowired;
import org.spring framework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.spring framework.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("/index")
public class HomeController {
    @Autowired
    private Environment evn;

    @GetMapping("getProperty")
    public String getProperties(@RequestParam("key") String key){
        String returnValue = "No value";
  
        String keyValue = evn.getProperty(key);
        
        if( keyValue!= null && !keyValue.isEmpty())
        {
         returnValue = keyValue;
        }
        return returnValue;
    }
}

Let’s see the output of this code in postman.

How to read the application.properties in spring boot

By using @ConfigurationProperties

  • To use this way we need to create a Java object where each class field matches the name of the key in the property file.

Let’s assume that the same application.properties file is used , since each of the property names starts with a prefix of app we will need to annotation our Java Bean.

Here is an example of a Java Class with @ConfigurationProperties annotation

package com.task2.codespeedy.springbootcodespeedy.services;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties("app")
public class AppProperties {
    
    private String appTitle;
    private String appdescription;
    public String getAppTitle() {
        return appTitle;
    }
    public void setAppTitle(String appTitle) {
        this.appTitle = appTitle;
    }
    public String getAppdescription() {
        return appdescription;
    }
    public void setAppdescription(String appdescription) {
        this.appdescription = appdescription;
    }
}

 

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("index")
public class HomeController {
 
 
 @Autowired
 AppProperties myAppProperties;
 
 
 @GetMapping("/apptitle")
 public String AppTitle()
 {
  return myAppProperties.getTitle();
 }
 
 @GetMapping("/appdescription")
 public String AppDescription()
 {
  return myAppProperties.getDescription();
 }
 
}

As soon as we have an instance of the AppProperties class then with the help of getters and setters we can easily get the property values.

By using @Value annotation

This is another simple way to read the application.properties file. We simply use the @Value annotation providing the name of the property which we want to fetch from the application.propeties file.

For eg. lets assume gettting the value of app.title property then we write the annotation as:

  @Value("${app.title}")
  private String serverport;

Code:
package com.task2.codespeedy.springbootcodespeedy.controller;

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("/index")
public class HomeController {
    
      @Value("${app.title}")
      private String serverport;

      @GetMapping("/value")
      public String serverPort(){
        if(serverport!=null && !serverport.isEmpty()){
            return serverport;
        }
        return "No value";
      }
}

Lets see the output of the value annotation in Postman.

How to read the application.properties in spring boot

And this is it! Three very simple ways to read application properties in you Spring Boot app

I hope this tutorial will help a lot of developers all around the world .

Leave a Reply

Your email address will not be published.