0
有一個屬性文件和必要的context:properties-placeholder
配置...註解與Spring @Value元註釋不注入價值
someproperty=somevalue
在一個Spring bean,我們可以有:
@Value("${someproperty}")
private String someProperty;
在哪裏someProperties值將是"somevalue"
。
我想創建一個新的註釋@Refreshable
與@Value
作爲元註釋,使其行爲與使用@Value
直接相同。由於@Value
需要一個值我硬編碼"default"
希望@Refreshable's value()
將覆蓋它:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@Value("default")
public @interface Refreshable {
String value();
}
然後,當我使用新的註釋在bean中,我期待它能夠正常工作,注射申報價值"somevalue"
:
@Refreshable("${someproperty}")
private String someProperty;
但我沒有得到${someproperty}
的值,但「默認」。想法?
您使用這個值來設置你的註釋org.springframework.beans.factory.annotation.Value值:要覆蓋@值的價值就要用
@AliasFor
。相反,它應該是這樣的默認值 - >公共字符串值()默認「默認」; – Avis關鍵是我不介意初始化的「默認」值。它只是在那裏,所以'public @interface Refreshable'編譯沒有錯誤。我真正想要的是它的價值來自實際使用新註釋'@Refreshable(「$ {someproperty}」)''。但是,有了這個,$ {someproperty}佔位符沒有解析。 – codependent
你可以把你的完整代碼顯示如何配置/打印someProperty的值,應該只是一些初始化問題。 – Avis