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 application.properties file in this project.
ReadPropertyFileInSpringBootDemoApplication.java
package com.programinjava.learn; import javax.annotation.Resource; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class ReadPropertyFileInSpringBootDemoApplication implements CommandLineRunner { @Resource ReadPropertyFileClass readPropertyFileClass; public static void main(String[] args) { SpringApplication.run(ReadPropertyFileInSpringBootDemoApplication.class, args); } @Override public void run(String... args) throws Exception { // TODO Auto-generated method stub readPropertyFileClass.showPropertyKeyValue(); } }
ReadPropertyFileClass.java
package com.programinjava.learn; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @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); } }
application.properties
Explanation:
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 :
- Common Elements in Two Sorted Arrays Program in java
- Most Frequent Occurring Item in an Array program in java
- Program in java for Array Rotation
- Program to get all the substring of the string in java
- Quick Sort Program in java using Array
- Java Interview : Heap Sort Implementation using Array
- Bubble Sort using array Program in Java
- Selection Sort Program Implementation Program in java
- Insertion Sort Program in Java using Array
hope you like this article , please share it with your friends and colleagues.
Thanks for reading
Thanks for reading
0 comments:
Post a Comment