Monday, April 16, 2018

How to read value from property file in spring boot ?

Spring boot came to remove the boiler code as well as to make the deployment easy and also make so many functionality very easy.

In earlier article we have talk about how to read the value from the property file in  java , now in this article we will talk about how to read property file in spring boot project.

You can download the project from GitHub
Lets look for the project structure.

There are two Class file and one file in this project.

package com.programinjava.learn;

import javax.annotation.Resource;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class ReadPropertyFileInSpringBootDemoApplication  implements CommandLineRunner {

 ReadPropertyFileClass readPropertyFileClass;
 public static void main(String[] args) {, args);

 public void run(String... args) throws Exception {
  // TODO Auto-generated method stub

package com.programinjava.learn;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

public class ReadPropertyFileClass {
 @Value("${propertykey1}")  // this annotation is used to read the property file value in spring boot
 String propertyKey;
 public void showPropertyKeyValue() {
  System.out.println("The Value of Property File key is "+propertyKey);


Result :- 

Spring boot provide the annotation @Value to read the property value from properties file.

what you need to do is you just need to use @Value("${<property-key>}")  where property-key will be replace by your property key whatever it is.
ex- @Value({"${key1}")
       String keyValue;

Now when you access the keyValue , it will give you the Value of the key defined in properties file.

Hope this is helpful for you to see how to read property file in string boot.

Also read :
hope you like this article , please share it with your friends and colleagues.

Thanks for reading


Post a Comment