[英]how to inject into a bean the values of a properties file read from controller?
我想使用控制器從.properties文件中讀取屬性,並在jsp文件中顯示其值,這是一個使用依賴注入的視圖,通過將檢索到的屬性存儲在pojo中。
為此使用PropertyPlaceholderConfigurer 。 屬性將由彈簧加載,因此您的控制器無需執行此操作。 您可以將屬性直接注入視圖中。
嘗試這個
@Component
class MyComponent {
@Property(key = "proo.xmlurl")
public void setUrlString(String urlStr) {
try {
this.url = new URL(urlStr);
} catch(MalformedURLException e) {
throw new IllegalArgumentException(urlStr + " is not a valid http url", e);
}
}
}
在你的屬性文件中放這個
proo.xmlurl=${proo.xmlurl}
AppContext可以有:
<context:property-placeholder location="classpath:my.properties" ignore-unresolvable="true"/>
控制器可以有這個
@Value("${language}")
private String language;
@Value("${allLanguages}")
private String allLanguages;
其中屬性文件包含此屬性或類似內容
language = java
alllanguages = java and \
c++
somethingelse = whatever
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.