15

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?

Boris the Spider
  • 59,842
  • 6
  • 106
  • 166
Alienpenguin
  • 967
  • 1
  • 9
  • 28
  • Looks like a [duplicate](https://stackoverflow.com/questions/25855795/spring-boot-and-multiple-external-configuration-files). Not importing but loading multiple files - does that answer your question? – Boris the Spider Sep 21 '16 at 12:09
  • Also, consider using [YAML](http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-yaml) rather than `properties` - this gives a much more structured document. – Boris the Spider Sep 21 '16 at 12:11

3 Answers3

14

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

Niraj Sonawane
  • 10,225
  • 10
  • 75
  • 104
3

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
Thirumal
  • 8,280
  • 11
  • 53
  • 103
  • 2
    This seems to be possible in Spring Boot 2.4 now: https://spring.io/blog/2020/08/14/config-file-processing-in-spring-boot-2-4#importing-additional-configuration –  Dec 04 '20 at 11:40
1
spring.config.import: file:${CLOUDJAVA_ROOT}/config/application.yaml

Imports are processed as they are discovered, and are treated as additional documents inserted immediately below the one that declares the import. Values from the imported file will take precedence over the file that triggered the import.

See spring-boot docs: Importing Additional Data

Grigory Kislin
  • 16,647
  • 10
  • 125
  • 197