In most cases, our applications do not use a single properties file. Why? One, we do not want to have a huge configuration file. Second, modular configuration – we can just swap out parts of the configuration and swap in with another. This awesome post shows how to read properties from multiple properties files in Spring Boot and it will help you sleep like a baby tonight!
Requirements
We use the following.
- Java 8
- Spring Boot 1.5.8.RELEASE
- Spring Initialzr
- IntelliJ IDEA or Eclipse
Multiple Properties Use Case
If we want to read multiple properties files, then we need more than one properties file. For this post, we have application.properties , config001.properties , and config002.properties .
application.properties
This file has this content.
1 | com.turreta.app.descripion=This application reads from multiple .properties files in Spring |
config001.properties
While this has the following.
1 2 3 | config001.field001=This is field001 from config001.properties config001.field002=This is field002 from config001.properties config001.field003=This is field003 from config001.properties |
config002.properties
This file contains the following.
1 2 3 | config002.field001=This is field001 from config002.properties config002.field002=This is field002 from config002.properties config002.field003=This is field003 from config002.properties |
For simplicity, we place these files in the resources directory in the Maven project.
A Singleton in Spring Boot
We use a singleton bean whose properties map to entries in 3 separate properties files. This is the main class to read properties from multiple properties files into an object in Spring Boot.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | package com.turreta.spring.multiple.properties.demo; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; @Component @PropertySource({"classpath:config001.properties", "classpath:config002.properties"}) public class AllConfig { @Value("${config001.field001}") private String config001Field001; @Value("${config001.field002}") private String config001Field002; @Value("${config001.field003}") private String config001Field003; @Value("${config002.field001}") private String config002Field001; @Value("${config002.field002}") private String config002Field002; @Value("${config002.field003}") private String config002Field003; @Value("${com.turreta.app.descripion}") private String appDescription; public String getConfig001Field001() { return config001Field001; } public String getConfig001Field002() { return config001Field002; } public String getConfig001Field003() { return config001Field003; } public String getConfig002Field001() { return config002Field001; } public String getConfig002Field002() { return config002Field002; } public String getConfig002Field003() { return config002Field003; } public String getAppDescription() { return appDescription; } } |
We use @PropertySource and locations of the files in the classpath. The application.properties is not used because it is implicitly included in the PropertySource by Spring Boot. Alternatively, we could use PropertySourced.
1 2 3 4 5 6 | @PropertySources( { @PropertySource("classpath:config001.properties"), @PropertySource("classpath:config002.properties") } ) |
Multiple Properties Demo and Test
We have a main class that displays the values from the bean.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | package com.turreta.spring.multiple.properties.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ApplicationContext; @SpringBootApplication public class ComTurretaSpringMultiplePropertiesDemoApplication { public static void main(String[] args) { ApplicationContext ctxt = SpringApplication.run( ComTurretaSpringMultiplePropertiesDemoApplication.class, args); AllConfig allConfig = ctxt.getBean(AllConfig.class); System.out.println(allConfig.getConfig001Field001()); System.out.println(allConfig.getConfig001Field002()); System.out.println(allConfig.getConfig001Field003()); System.out.println(allConfig.getConfig002Field001()); System.out.println(allConfig.getConfig002Field002()); System.out.println(allConfig.getConfig002Field003()); System.out.println(allConfig.getAppDescription()); } } |
This outputs the following.
1 2 3 4 5 6 7 8 9 10 11 | ... 2017-10-20 22:36:39.697 INFO 1184 --- [ main] aSpringMultiplePropertiesDemoApplication : Started ComTurretaSpringMultiplePropertiesDemoApplication in 2.294 seconds (JVM running for 3.229) This is field001 from config001.properties This is field002 from config001.properties This is field003 from config001.properties This is field001 from config002.properties This is field002 from config002.properties This is field003 from config002.properties This application reads from multiple .properties files in Spring 2017-10-20 22:36:39.703 INFO 1184 --- [ Thread-2] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation ... |
The codes are available here.