java 如何在 .yml 文件中使用属性占位符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43148748/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use Property placeholders in .yml file
提问by Jesse
I am working with Java and spring boot. I was wondering how to add Property placeholders into .yml
files. I've found some crisp example but I am not sure where are the Property placeholders is being instantiated in. Is it in system env variables, a file, etc..?
我正在使用 Java 和 Spring Boot。我想知道如何将属性占位符添加到.yml
文件中。我找到了一些清晰的例子,但我不确定在哪里实例化属性占位符。它是在系统环境变量、文件等中吗?
Bootstrap.yml
引导程序.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
User is using Property placeholders, but where did the user declared it? Where is this .yml reading the values from? (same question as above) Is there a document that explains the connection?
用户正在使用属性占位符,但用户在哪里声明它?这个 .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.
这个 Web 应用程序将使用“cf push”推送到云代工厂,它将自动选择 manifest.yml 文件进行配置。如果可能的话,云代工厂的例子会很棒。
Understanding/ Sample Application.properties file
理解/示例 Application.properties 文件
app.name=MyApp
app.description=${app.name}
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?
用户能够使用 ${app.name} 因为它是定义的。我对上面的例子感到困惑。用户如何以及在哪里获得“${my.stored.files.username}。在哪里定义?我认为它会在 system.properties 或环境变量中。谁能确认?
回答by Jesse
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.
经过深入研究,我发现当我在 .yml 文件中使用占位符时,它会从环境变量中读取这些值。这是我一开始的理论的一部分,但没有人证实。
Answer for local environment
当地环境的答案
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
*In environment variables *
*在环境变量中*
set key as: my.stored.files.username
set value as: UsernameSample
Then
然后
When you run application, yml will read like so.
当您运行应用程序时,yml 会像这样读取。
config:
username: ${my.stored.files.username}
//gets replaced with UsernameSample
This is the link that solved my problem link
这是解决我的问题链接的链接
For Cloudfoundry
对于 Cloudfoundry
You would have to create cups or manually add these variables onto the service.
您必须创建杯子或手动将这些变量添加到服务中。
回答by gavenkoa
SpringApplication loads properties from application.properties files in the following locations and adds them to the Spring Environment:
SpringApplication 从以下位置的 application.properties 文件加载属性并将它们添加到 Spring Environment:
- A /config subdirectory of the current directory
- The current directory
- A classpath /config package
- The classpath root
- 当前目录的 /config 子目录
- 当前目录
- 一个类路径 /config 包
- 类路径根
The list is ordered by precedence (properties defined in locations higher in the list override those defined in lower locations).
该列表按优先级排序(在列表中较高位置定义的属性覆盖在较低位置定义的属性)。
You can also use YAML ('.yml') files as an alternative to '.properties'.
您还可以使用 YAML ('.yml') 文件作为 '.properties' 的替代文件。
If you do not like application.properties as the configuration file name, you can switch to another file name by specifying a spring.config.name environment property. You can also refer to an explicit location by using the spring.config.location environment property (which is a comma-separated list of directory locations or file paths). The following example shows how to specify a different file name:
如果你不喜欢 application.properties 作为配置文件名,你可以通过指定一个 spring.config.name 环境属性来切换到另一个文件名。您还可以使用 spring.config.location 环境属性(以逗号分隔的目录位置或文件路径列表)来引用显式位置。以下示例显示如何指定不同的文件名:
$ java -jar myproject.jar --spring.config.name=myproject
The following example shows how to specify two locations:
以下示例显示了如何指定两个位置:
$ java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties
spring.config.name and spring.config.location are used very early to determine which files have to be loaded. They must be defined as an environment property (typically an OS environment variable, a system property, or a command-line argument).
spring.config.name 和 spring.config.location 很早就用于确定必须加载哪些文件。它们必须定义为环境属性(通常是操作系统环境变量、系统属性或命令行参数)。
If spring.config.location contains directories (as opposed to files), they should end in / (and, at runtime, be appended with the names generated from spring.config.name before being loaded, including profile-specific file names). Files specified in spring.config.location are used as-is, with no support for profile-specific variants, and are overridden by any profile-specific properties.
如果 spring.config.location 包含目录(而不是文件),它们应该以 / 结尾(并且,在运行时,在加载之前附加从 spring.config.name 生成的名称,包括特定于配置文件的文件名)。spring.config.location 中指定的文件按原样使用,不支持特定于配置文件的变体,并被任何特定于配置文件的属性覆盖。
Config locations are searched in reverse order. By default, the configured locations are classpath:/,classpath:/config/,file:./,file:./config/. The resulting search order is the following:
配置位置以相反的顺序搜索。默认情况下,配置的位置是 classpath:/,classpath:/config/,file:./,file:./config/。结果搜索顺序如下:
file:./config/
file:./
classpath:/config/
classpath:/
file:./config/
file:./
classpath:/config/
classpath:/
When custom config locations are configured by using spring.config.location, they replace the default locations. For example, if spring.config.location is configured with the value classpath:/custom-config/,file:./custom-config/, the search order becomes the following:
当使用 spring.config.location 配置自定义配置位置时,它们会替换默认位置。例如,如果使用值 classpath:/custom-config/,file:./custom-config/ 配置 spring.config.location,则搜索顺序变为如下:
file:./custom-config/
classpath:custom-config/
file:./custom-config/
classpath:custom-config/
Alternatively, when custom config locations are configured by using spring.config.additional-location, they are used in addition to the default locations. Additional locations are searched before the default locations. For example, if additional locations of classpath:/custom-config/,file:./custom-config/ are configured, the search order becomes the following:
或者,当使用 spring.config.additional-location 配置自定义配置位置时,除了默认位置之外,还会使用它们。在默认位置之前搜索其他位置。例如,如果配置了 classpath:/custom-config/,file:./custom-config/ 的附加位置,则搜索顺序将变为以下内容:
file:./custom-config/
classpath:custom-config/
file:./config/
file:./
classpath:/config/
classpath:/
file:./custom-config/
classpath:custom-config/
file:./config/
file:./
classpath:/config/
classpath:/
This search ordering lets you specify default values in one configuration file and then selectively override those values in another. You can provide default values for your application in application.properties (or whatever other basename you choose with spring.config.name) in one of the default locations. These default values can then be overridden at runtime with a different file located in one of the custom locations.
这种搜索顺序允许您在一个配置文件中指定默认值,然后在另一个配置文件中选择性地覆盖这些值。您可以在默认位置之一的 application.properties(或您使用 spring.config.name 选择的任何其他基本名称)中为您的应用程序提供默认值。然后可以在运行时使用位于自定义位置之一的不同文件覆盖这些默认值。
回答by user1608528
Please use {{your key}} as a place holder in case of .yml file
如果是 .yml 文件,请使用 {{your key}} 作为占位符