How to Read value From in spring boot

In Earlier Tutorial We have Learn How to Change the default port in Spring Boot , Here we see how we can read in spring boot . We have different ways for reading values from application properties ,here we will discuss three ways for reading  keys from application.propeties file as following

How to read value from Using @Value

Now we see how to read properties file in spring using annotation @Value . If you are in new spring boot and then i will recommencement to you first read how create project in Spring boot . Firstly we will add below values in file of our project.


Now we will create java Class with name EmailService where we map these  key to class’s field using  @Value as below .

package com.javavogue.demo.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

public class EmailService {
	 private String username;
	 private String pwd;

	 private EmailConfig emailConfig;
	public void sendEmail(){
	System.out.println("reading value from propertes file using @value annotation");
	System.out.println("username ="+ username);
	System.out.println("pwd ="+ pwd);

How to read value from Using @ConfigurationProperties

In this we explain How to load properties using Spring Boot @ConfigurationProperties .In this way we will create a plain java object where each class field name same as the key of Because In file we have email string as prefix of key so that we will use email string with @ConfigurationProperties annotation . In below we have given Spring Boot @ConfigurationProperties example

package com.javavogue.demo.service;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;

import org.springframework.stereotype.Component;

public class EmailConfig {
  private String   username;
    private String  pwd;
    public String getUsername() {
		return username;
	public void setUsername(String username) {
		this.username = username;
	public String getPwd() {
		return pwd;
	public void setPwd(String pwd) {
		this.pwd = pwd;
	public String toString() {
		return "EmailConfig [username=" + username + ", pwd=" + pwd + "]";

How to read Using using Environment object

Now we will read value from application.propeties file using Environment object. We will pass key in getProperty(“Key”)  method on environment object as

package com.javavogue.demo.controller;

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

import com.javavogue.demo.service.EmailService;

public class EmailController {
	private Environment env;

	public void sendMail(){		
System.out.println("values from application properties using Environment ");
System.out.println("username ="+ env.getProperty("email.username"));
System.out.println("pwd ="+ env.getProperty("email.pwd"));

In This tutorial we have given three method for reading properties file .

A Guide to Spring Boot

Spring boot doc

Leave a Reply

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

7 + 1 =