Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot application.properties extends another property file

I have few properties in one property file which I want to inherit to application properties. Please let know if that is possible in spring boot.

something like

application.properties
----------------------
extends = otherproperty.properties
property1 = value1
property2= value2
property3 = value3

otherproperty.properties
------------------------
property4=value4
property5 = value5

When the spring boot app loads, I want all 5 properties should be loaded and available using @Value annotation. The spring boot automatically picks the application.properties in classpath and I don't have applicaitoncontext xml or any property loader code.

Thanks in advance.

like image 225
skumar Avatar asked Oct 21 '15 15:10

skumar


1 Answers

You can use Profiles for that.

With application.properties files, you can define a file for every Profile, like this:

application.properties # This is the main file
spring.profiles=p1,p2,p3
prop-main=hi

application-p1.properties # This is the file with p1 properties
property1=patata

application-p2.properties # This is the file with p2 properties
property2=catsup

application-p3.properties # This is the file with p3 properties
property3=helloworld

Spring will translate the files and use it like this:

application.properties # This is the main file
spring.profiles=p1,p2,p3
prop-main=hi
property1=patata
property2=catsup
property3=helloworld

This solution works, but you have to keep a separate file for each group.

There is another way, you can use a single YAML file instead of multiple properties files. Just replace the application.properties with an application.yml and do something like this:

server:
    address: 192.168.1.100
---
spring:
    profiles: development
server:
    address: 127.0.0.1
---
spring:
    profiles: production
server:
    address: 192.168.1.120

You can read the reference documentation for more info about Using YAML instead of Properties and about Multi-profile YAML documents.

like image 97
ESala Avatar answered Sep 20 '22 10:09

ESala