how to read value from in spring boot

In this post we will learn how to read in spring boot . In this post we will learn these point

  • How to read value from Using @Value
  • How to read value from Using @ConfigurationProperties
  • How to read Using using Environment object

Now we explain spring boot example step by step.

How to read value from Using @Value

Firstly we will learn how read properties file in spring using annotation @Value . If you new spring boot and then first learn  create project in Spring boot . Now will create file with these values .


Now we will create  Class EmailService where read these  value  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

Now we explain How to load properties using Spring Boot @ConfigurationProperties .In this method just create a plain java object where  each class field to key of Because email is prefix so will use email prefix with @ConfigurationProperties annotation . Now are given Spring Boot @ConfigurationProperties example below.

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("reading value from application properties file  using Environment ");
		System.out.println("username ="+ env.getProperty("email.username"));
		System.out.println("pwd ="+ env.getProperty("email.pwd"));


Notice: compact(): Undefined variable: limits in /home/javaprof/public_html/wp-includes/class-wp-comment-query.php on line 853

Notice: compact(): Undefined variable: groupby in /home/javaprof/public_html/wp-includes/class-wp-comment-query.php on line 853

Leave a Reply

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

2 + 3 =