【问题标题】:How to use Property placeholders in .yml file如何在 .yml 文件中使用属性占位符
【发布时间】:2017-08-26 04:50:23
【问题描述】:

我正在使用 Java 和 Spring Boot。我想知道如何将属性占位符添加到 .yml 文件中。我找到了一些清晰的示例,但我不确定属性占位符在哪里被实例化。是在系统环境变量、文件等中吗?

Bootstrap.yml

spring:
  cloud:
    config:
      username: ${my.stored.files.username}
      password: ${my.stored.files.password}
      label: ${spring.cloud.find.label}
      uri: ${spring.cloud.config.uri}
      enabled: false
      failFast: true

用户正在使用属性占位符,但用户在哪里声明了它们? 这个 .yml 从哪里读取值? (与上述相同的问题) 是否有解释连接的文件?

这个 web 应用程序将使用“cf push”推送到 Cloud Foundry,它会自动选择 manifest.yml 文件进行配置。如果可能的话,一个云代工厂的例子会很棒。

了解/示例 Application.properties 文件

app.name=MyApp
app.description=${app.name} 

用户能够使用 ${app.name} 因为它已定义。我对上面的例子感到困惑。用户如何以及在何处获得“${my.stored.files.username}. 这是在哪里定义的?我认为它会在 system.properties 或环境变量中。谁能确认一下?

【问题讨论】:

  • 谢谢@DaveJarvis 但我认为这不是我想要的。
  • 您是否尝试过在 YAML 或应用程序使用的其他配置文件中搜索“用户名”?在不知道文件内容或应用程序的文件夹结构的情况下,任何人都很难回答这个问题。不过,您应该能够搜索文件以找到定义变量的位置。
  • @DaveJarvis 这就是我想要了解自己的。我知道这是一个 bootstrap.yml 文件。我只是不明白占位符从哪里接收他们的值。我按照您的方法搜索了“用户名”。我看到 build.gradle 有 "username = "${artifactory_user}"。但是 "artifactory_user" 不会导致其他任何地方。

标签: java spring-boot yaml manifest string-interpolation


【解决方案1】:

经过深入研究,我发现当我在 .yml 文件中使用占位符时,它会从环境变量中读取该值。这是我一开始的理论的一部分,但没有人证实。

当地环境的答案

spring:
  cloud:
    config:
      username: ${my.stored.files.username}
      password: ${my.stored.files.password}
      label: ${spring.cloud.find.label}
      uri: ${spring.cloud.config.uri}
      enabled: false
      failFast: true

*在环境变量中*

set key as: my.stored.files.username
set value as: UsernameSample

那么

当你运行应用程序时,yml 会这样读取。

    config:
      username: ${my.stored.files.username}
                //gets replaced with UsernameSample

这是解决我的问题的链接link

对于 Cloudfoundry

您必须创建杯子或手动将这些变量添加到服务中。

【讨论】:

  • @IslamAzab 当您在本地运行它时。 1) 按 windows 键 2) 输入“环境变量” 3) 按编辑环境变量(这是您设置键/值的地方)
  • 不仅使用 Env,还可以查看其他选项:stackoverflow.com/questions/54883144/…
【解决方案2】:

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-application-property-files

SpringApplication 从以下位置的 application.properties 文件中加载属性并将它们添加到 Spring 环境中:

  • 当前目录的 /config 子目录
  • 当前目录
  • 类路径 /config 包
  • 类路径根

列表按优先级排序(在列表中较高位置定义的属性会覆盖在较低位置定义的属性)。

您还可以使用 YAML ('.yml') 文件来替代“.properties”。

如果你不喜欢 application.properties 作为配置文件名,你可以通过指定 spring.config.name 环境属性来切换到另一个文件名。您还可以使用 spring.config.location 环境属性(以逗号分隔的目录位置或文件路径列表)来引用显式位置。以下示例显示了如何指定不同的文件名:

 $ java -jar myproject.jar --spring.config.name=myproject

以下示例显示了如何指定两个位置:

$ java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties

spring.config.name 和 spring.config.location 很早就用于确定必须加载哪些文件。它们必须定义为环境属性(通常是操作系统环境变量、系统属性或命令行参数)。

如果 spring.config.location 包含目录(而不是文件),它们应该以 / 结尾(并且在运行时,在加载之前附加从 spring.config.name 生成的名称,包括特定于配置文件的文件名称)。 spring.config.location 中指定的文件按原样使用,不支持特定于配置文件的变体,并被任何特定于配置文件的属性覆盖。

以相反的顺序搜索配置位置。默认情况下,配置的位置是classpath:/,classpath:/config/,file:./,file:./config/。结果搜索顺序如下:

  • file:./config/
  • file:./
  • classpath:/config/
  • classpath:/

当使用 spring.config.location 配置自定义配置位置时,它们会替换默认位置。例如,如果spring.config.location配置了值classpath:/custom-config/,file:./custom-config/,则搜索顺序如下:

  • file:./custom-config/
  • classpath:custom-config/

或者,当使用 spring.config.additional-location 配置自定义配置位置时,除了默认位置之外,还会使用它们。在默认位置之前搜索其他位置。例如,如果配置了classpath:/custom-config/,file:./custom-config/的附加位置,则搜索顺序如下:

  • file:./custom-config/
  • classpath:custom-config/
  • file:./config/
  • file:./
  • classpath:/config/
  • classpath:/

这种搜索顺序允许您在一个配置文件中指定默认值,然后在另一个配置文件中选择性地覆盖这些值。您可以在默认位置之一的 application.properties(或您使用 spring.config.name 选择的任何其他基本名称)中为您的应用程序提供默认值。然后可以在运行时使用位于自定义位置之一的不同文件覆盖这些默认值。

【讨论】:

    【解决方案3】:

    在做了一些研究和实验后,我发现占位符既可以是环境变量,也可以是command line arguments。属性文件的语法也适用于 YAML 文件。 @Jesse 已经解释了环境变量。如果你说通过命令行参数:

    --my.stored.files.username=UsernameSample--username=UsernameSample,配置属性将按预期填充

    my:
     stored:
      files:
       username: ${username:defaultUsername}
    

    我希望这可能对遇到类似问题的人有所帮助。

    【讨论】:

      【解决方案4】:

      如果是 .yml 文件,请使用 {{your key}} 作为占位符

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-06-20
        • 2017-07-09
        • 2021-04-03
        • 1970-01-01
        • 1970-01-01
        • 2016-01-11
        • 2013-09-02
        相关资源
        最近更新 更多