如何在 .yml 文件中使用 属性 占位符

How to use Property placeholders in .yml file

我正在使用 Java 和 spring 启动。我想知道如何将 属性 占位符添加到 .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 或环境变量中。谁能确认一下?

经过深入研究,我发现当我在 .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 link

对于 Cloudfoundry

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

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

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

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

  • 当前目录的/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 很早就使用来确定必须加载哪些文件。它们必须定义为环境 属性(通常是 OS 环境变量、系统 属性 或命令行参数)。

如果 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 选择的任何其他基本名称)中为您的应用程序提供默认值。然后可以在运行时使用位于自定义位置之一的不同文件覆盖这些默认值。

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

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

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

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