how to read value from application.properties in spring boot

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

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

Now we explain spring boot application.properties example step by step.

How to read value from application.properties 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 application.properties file with these values .

Application.properties

email.username=javavogue
email.pwd=12345

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;

@Service
public class EmailService {
	
	 @Value("${email.username}")
	 private String username;
	 
	 @Value("${email.pwd}")
	 private String pwd;

	 @Autowired
	 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 application.properties 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 application.properties. Because email is prefix so will use email prefix with @ConfigurationProperties annotation . Now are given Spring Boot @ConfigurationProperties example below.

EmailConfig.java

package com.javavogue.demo.service;

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

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

@Component
@ConfigurationProperties("email")
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;
	}
		
	@Override
	public String toString() {
		return "EmailConfig [username=" + username + ", pwd=" + pwd + "]";
	}
	
}

How to read  application.properties 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;

@RestController
public class EmailController {
	
	@Autowired
	private Environment env;

	@GetMapping("/sendmail")
	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"));
	}
}

 

Leave a Reply

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

33 − 29 =