Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to include properties file in another properties file?

I'd like to avoid cluttering the application.properties file with lots of things than, in my opinion, would be better in a separate file.

application.properties should be something like

@include module1.properties
@include module1.properties
...
###################################
######### Spring Misc #############
###################################

# Direct log to a log file
logging.file=/tmp/kmp-manager.log

#local listening port
server.port=8082

spring.profiles=nr_dev nr_testing production
spring.profiles.active=production

spring.datasource.platform=postgresql

java.security.egd=file:/dev/./urandom

Is this at all possibile? If not, what would be a sane way to avoid cluttering?

like image 334
Alienpenguin Avatar asked Sep 21 '16 12:09

Alienpenguin


People also ask

Can I reference another property in a properties file?

In Spring property (or yaml) files we can reference other properties using the ${..} syntax.

Can we have two properties file in Spring Boot?

properties” property file, as it was automatically built inside the Spring boot application when the project is initially created. We can create and maintain multiple property files within “Resources” module, where we will be able to define different types of property values in different files.

How do you append to a properties file in Java?

Just do not open the file in append mode. You read existing properties from the file and the write them again. If you append to the file, all the contents of the Properties object will be appended since this is what you asked for. FileOutputStream fileOut = new FileOutputStream(file,true);


2 Answers

Spring Boot Spring Boot 2.4 has added a feature for importing

We can now use spring.config.import=developer.properties to import other file. Check this blog post for more details

like image 107
Niraj Sonawane Avatar answered Oct 26 '22 23:10

Niraj Sonawane


It's possible in YML file and the configuration are very simple

EXAMPLE:

To include properties of application-DATABASE.yml file in application.yml, use

spring:
  profiles:
    include: DATABASE

[EDIT - for 2.4.0 and above]

spring.profiles.group.prod=DATABASE

OR

Add the file name in application.properties

spring.config.import=classpath:application-DEV.yml,classpath:application-UDEV.yml,classpath:application-PRO.yml,classpath:application-SBA.yml
like image 20
Thirumal Avatar answered Oct 27 '22 00:10

Thirumal