How to read properties file in spring boot

What you will learn here about spring boot

  • How to read properties file in spring boot

How to read properties file in spring boot

Please follow the following steps to know how to read properties file in spring boot
1)First create a simple maven project

2)Please add web starter dependency in POM.xml:base

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>

starter web dependency

3)Please add properties in your application.properties
how to read properties file in spring boot

4)Please follow the following steps which is shown below

  1. Autowire Environment object
  2. Use getProperty() to get value from properties file

spring boot read data from properties file

5)Now please run the spring boot application

6)Now please to browser and hit the url for getting name. You will see following kind of output in the browser
Name output

7)Now please to browser and hit the url for getting email. You will see following kind of output in the browser
email output

Frequently Asked Question

1)How to read data from different properties file data?

Answer: Assume you have a test.properties file which is in resources folder. To read data from test.properties file just put @PropertySource(“classpath:test.properties”) on the top of your class and then use Environment object.

You may also like...