有没有一种方法可以将属性文件包含在另一个属性文件中?

Ali*_*uin 6 spring properties spring-boot

我想避免使application.properties文件杂乱无章,比我认为单独存放文件更好。

application.properties 应该是这样的

@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
Run Code Online (Sandbox Code Playgroud)

这完全有可能吗?如果没有,那么避免混乱的明智方法是什么?

Nir*_*ane 16

Spring Boot Spring Boot 2.4增加了导入的功能

我们现在可以用来spring.config.import=developer.properties导入其他文件。查看此博客文章了解更多详细信息


Thi*_*mal 3

可以在YML文件中设置,而且配置非常简单

例子:

要在 中包含application-DATABASE.yml文件的属性application.yml,请使用

spring:
  profiles:
    include: DATABASE
Run Code Online (Sandbox Code Playgroud)

[编辑 - 适用于 2.4.0 及更高版本]

spring.profiles.group.prod=DATABASE

或者

添加文件名在application.properties

spring.config.import=classpath:application-DEV.yml,classpath:application-UDEV.yml,classpath:application-PRO.yml,classpath:application-SBA.yml
Run Code Online (Sandbox Code Playgroud)

  • 现在,这在 Spring Boot 2.4 中似乎是可能的:https://spring.io/blog/2020/08/14/config-file-processing-in-spring-boot-2-4#importing-additional-configuration (2认同)