【问题标题】:Reference properties from other Spring PropertySource来自其他 Spring PropertySource 的引用属性
【发布时间】:2020-10-29 11:31:45
【问题描述】:

在 Spring 中,我可以将其他一些属性值作为属性源中的变量引用

base:
  hostName: foo.baa.com

services
  serviceUrl: http://${base.hostName}/fooService

这将返回 http://foo.baa.com/fooService。但前提是base.hostName 是在同一个文件中定义的。如果它在另一个 PropertySource 中定义,我会得到 http://${base.hostName}/fooService

我可以在多个PropertySources 上运行它吗?

编辑:这只是一个愚蠢的错字。您可以完美地引用其他 PropertySources 中定义的变量

【问题讨论】:

  • other 属性文件在哪里?它是被其他应用程序使用还是您打算直接读取文件?
  • 我不得不撤回我的问题,只是我的代码中有一个愚蠢的错误。您可以完美地包含在其他 PropertySources 中定义的变量
  • 您能否提及您是如何做到的,因为不清楚您打算如何包含它?
  • 我有一个包含所有base.* 属性的属性文件和一个包含service.* 属性的属性文件。两者都由不同的 PropertySourcs 加载,但都在同一个 ApplicationContext 中。如前所述:如果做对了,就会奏效。

标签: java spring


【解决方案1】:

在 Spring 中没有简单的方法可以将另一个 yml 导入另一个... 但是,您可以使用 spring.profiles.include 属性来解决问题。更多解释在这里:Spring "spring.profiles.include" overrides

【讨论】:

    猜你喜欢
    • 2021-08-28
    • 2014-07-21
    • 1970-01-01
    • 2016-09-02
    • 1970-01-01
    • 2021-09-30
    • 2017-05-23
    • 2020-07-20
    • 1970-01-01
    相关资源
    最近更新 更多