0

Read from multiple .properties files in Spring Boot

This post demonstrates how to read properties from multiple .properties files in Spring Boot.

Requirements

  1. Java 8
  2. Spring Boot 1.5.8.RELEASE
  3. Spring Initialzr
  4. IntelliJ IDEA or Eclipse

Use Case

We have the following .properties files to read from and into an object’s properties.

application.properties

This file has this content:

config001.properties

While this one has:

config002.properties

This file has:

All these files are located under the resources directory in the Maven project.

A Singleton bean

We use a singleton bean whose properties are mapped to entries in 3 separate .properties files.

We use @PropertySource and an array of locations of the files in the classpath. Notice application.properties is not specified as it is implicitly included in the @PropertySource by Spring Boot.

Alternatively, we could use @PropertySources.

Demo and Test

We have a main class that simply displays the values from the bean.

This outputs:

Download the codes

https://github.com/Turreta/Read-from-multiple-.properties-files-in-Spring-Boot