How to read property from application.properties file in Spring Boot

Home / How to read property from application.properties file in Spring Boot

As we know in every web application we define some property that we use in our code in a common file so that if we want to change the value in future we have to do changes in only one file. In Spring boot we define our property in application.properties file and to read this property from application.properties file spring boot provides two methods.

  1. Using @Value annotation.
  2. Using @ConfigurationProperties annotation.

In this blog we see both the methods. First we will see how to read property using @value annotation. To read the value using @Value annotaion first we define the property name in application.properties file say example we define a property name myname and value as vasu so to read myname property using @Value annotaion we use below syntax:

application.property

myname=vasu
@Value("${myname} ")
private String name;

Now Our class will look like below:

package com.example.demo.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class NameController {

	@Value("${myname} ")
	private String name;
	
	@GetMapping("/")
	public String test() {
		return "My Name is "+name;
	}
}

Now if you run your application and hit the URL http://localhost:8080/ you will get the response like below:

My Name is vasu

But it is a bad practice to read property values using @Value annotation. Suppose we have 10 property so to read this 10 property we have to use @Value annotation 10 times. So to avoid this we use @ConfigurationProperties annotaion.

To use @ConfigurationProperties annotation we define a class and annotated this class with @ConfigurationProperties anotation and we use a prefix with this @ConfigurationProperties annotaion like @ConfigurationProperties(“vasu”) now in our application.properties file we define property with this prefix like below:

vasu.value=true
vasu.message= coding is fun
vasu.number=1

Now create a class that annotated with @ConfigurationProperties(“vasu”) annotaion:

package com.example.demo.model;

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

@Component
@ConfigurationProperties("vasu")
public class MyModel {

	private boolean value;
	private String message;
	private int number;

	public MyModel() {
		super();
	}

	public boolean isValue() {
		return value;
	}

	public void setValue(boolean value) {
		this.value = value;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public int getNumber() {
		return number;
	}

	public void setNumber(int number) {
		this.number = number;
	}

}

Now Autowired this class in your controller and read the property file using getter method like below:

package com.example.demo.controller;

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.RestController;

import com.example.demo.model.MyModel;

@RestController
public class NameController {

	@Value("${myname} ")
	private String name;
	
	@Autowired
	private MyModel model;
	
	@GetMapping("/")
	public String test() {
		return "My Name is "+name
				+", i am always "+model.isValue()
				+", my messgage to you is  "+model.getMessage()
				+", i want to be number  "+model.getNumber();
	}
}

Now if your run your application and hit the URL http://localhost:8080/you will get the response like below:

My Name is vasu , i am always true, my messgage to you is coding is fun, i want to be number 1

Complete code on GitHub: Read Property in Spring Boot

Help Others, Please Share

About Author

2 Comments
  1. phone interview questions

    https://waterfallmagazine.com
    Ahaa, its fastidious discussion regarding this paragraph at this
    place at this website, I have read all that, so now me also commenting here.

  2. vietnam property

    Simply want to say your article is as astounding. The clearness in your post is just
    excellent and i could assume you’re an expert on this subject.
    Well with your permission allow me to grab your RSS feed to
    keep updated with forthcoming post. Thanks a million and please keep
    up the gratifying work.

Leave a Reply

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