Spring boot application.properties extends another properties file

I have several properties in one properties file that I want to inherit from the properties of the application. Please advise if this is possible when loading spring.

sort of

application.properties ---------------------- extends = otherproperty.properties property1 = value1 property2= value2 property3 = value3 otherproperty.properties ------------------------ property4=value4 property5 = value5 

When the spring boot application loads, I want all 5 properties to be loaded and accessible using the @Value annotation. Spring loading automatically selects application.properties in the classpath, and I don't have applicaitoncontext xml or any property loader code.

Thanks in advance.

+5
source share
3 answers

spring.profiles.include=p1,p2,p3 in application.properties file works for me

+4
source

You can use Profiles for this.

With application.properties files, you can define a file for each Profile , for example:

 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 them 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 must keep a separate file for each group.

There is another way: you can use only one YAML file instead of several properties files. Just replace application.properties with 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 help documentation for more information on Using YAML instead of Properties and Multidisciplinary YAML Documents .

+9
source

I had the same question and I found a good explained solution to mkyong. The guide explains how to use various Spring profile extensions using .porperties or .yaml . (Same approach as eSala .)

https://www.mkyong.com/spring-boot/spring-boot-profile-based-properties-and-yaml-example/

0
source

Source: https://habr.com/ru/post/1234205/


All Articles