簡體   English   中英

Spring boot YAML 配置:參數化鍵

[英]Spring boot YAML config : Parametred keys

spring boot 是否允許使用 YAML 使用參數化鍵?

參數化鍵的示例:

myapp.configured.key: This is your email > {0} And this is your name > {1}

在我的 Java 類中,我想做這樣的事情:

@Inject
private Environment env;//import org.springframework.core.env.Environment;


String email = "email@email.com"
String name = "User Name";

String key=env.getProperty("myapp.configured.key", email, name);

System.out.println(key);

輸出將是這樣的:

This is your email > email@email.com And this is your name > User Name

如果你的emailname也是配置鍵,你可以在你的消息中引用它們(但有點奇怪)

myapp.configured.email: email@example.com
myapp.configured.name: John Smith
myapp.configured.key: This is your email > ${myapp.configured.email} And this is your name > ${myapp.configured.name}

Environment沒有這樣的 API,作為一個 Spring Boot 用戶,你甚至不應該接觸那些東西(檢查@ConfigurationProperties以獲取配置的類型安全綁定)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM