【问题标题】:Is there a way to include properties file in another properties file?有没有办法将属性文件包含在另一个属性文件中?
【发布时间】:2017-01-29 16:21:45
【问题描述】:

我想避免在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

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

【问题讨论】:

  • 看起来像duplicate。不导入而是加载多个文件 - 这能回答您的问题吗?
  • 另外,考虑使用YAML 而不是properties - 这样可以提供更加结构化的文档。

标签: spring properties spring-boot


【解决方案1】:

YML文件中可以,配置很简单

示例:

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

spring:
  profiles:
    include: DATABASE

[编辑 - 适用于 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

【讨论】:

【解决方案2】:

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

我们现在可以使用spring.config.import=developer.properties 来导入其他文件。查看此blog 帖子了解更多详情

【讨论】:

    猜你喜欢
    • 2016-08-24
    • 2015-03-13
    • 1970-01-01
    • 2020-06-23
    • 1970-01-01
    • 2013-05-13
    • 1970-01-01
    • 2016-07-04
    • 2010-09-09
    相关资源
    最近更新 更多