簡體   English   中英

如何在動態屬性@Value中插入參數

[英]How to inject arguments in a dynamic property @Value

我在嘗試從資源屬性文件讀取屬性並通過Java代碼注入參數時遇到問題。

我正在使用Spring的@Value注釋。

像下面的示例一樣,假設我在屬性文件中有一個名為person.description的屬性,並且我想將名字作為參數注入。

#Properties file
person.description = {0} studies at the University of Johannesburg.

下面是調用屬性的方法(Java)。

public String doSomething(@Value("${person.description}") String description, String studentName) {...

我希望輸出如下:

約翰在約翰內斯堡大學學習。

就像您的財產一樣, person.description = {0} studies at the University of Johannesburg. ,那么如果返回此字符串,則表示該值已正確注入。 那么對我來說,您的函數返回此值是正常的。 如果要返回學生姓名,則可以return description.replace('{0}', studentName); 或類似的東西會工作嗎?

暫無
暫無

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

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