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

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

I am working with Java and spring boot. I was wondering how to add Property placeholders into .yml files. I've found some crisp examples but I am not sure where are the Property placeholders are being instantiated in. Is it in system env variables, a file, etc..?

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 从哪里读取值?(与上述相同的问题)是否有解释连接的文件?

User is using Property placeholders, but where did the user declared them? Where is this .yml reading the values from? (same question as above) Is there a document that explains the connection?

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

This web application will be pushed to cloud foundry using "cf push", Which will automatically pick manifest.yml file to configure. If possible a cloud foundry example would be great.

了解/示例 Application.properties 文件

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

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

User was able to use ${app.name} because it is defined. I am confused on the example above. How and where is the user getting "${my.stored.files.username}. Where is that being defined? I assumed it would be in system.properties or environment variables. Can anyone confirm?

推荐答案

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

After intensive research, I was able to find that when I use placeholders in .yml files it reads that values from environment variables. Which was part of my theory in the beginning, but no one has confirmed.

当地环境的答案

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

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

This is the link that solved my problem link

对于 Cloudfoundry

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

You would have to create cups or manually add these variables onto the service.

相关文章