問題描述
我正在使用 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.
這篇關于如何在 .yml 文件中使用屬性占位符的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!