我在application.properties以下文件如何在Spring中綁定一個字符串數組屬性?
some.server.url[0]=http://url
some.server.url[1]=http://otherUrl
如何我指的是使用@Bean方法裏面@Value anotation屬性的陣列?
我使用Java 6與Tomcat 7和Spring啓動1.4
我在application.properties以下文件如何在Spring中綁定一個字符串數組屬性?
some.server.url[0]=http://url
some.server.url[1]=http://otherUrl
如何我指的是使用@Bean方法裏面@Value anotation屬性的陣列?
我使用Java 6與Tomcat 7和Spring啓動1.4
您可以使用一個集合。
@Value("${some.server.url}")
private List<String> urls;
您也可以使用配置類和bean注入到你的其他類:
@Component
@ConfigurationProperties("some.server")
public class SomeConfiguration {
private List<String> url;
public List<String> getUrl() {
return url;
}
public void setUrl(List<String> url) {
this.url = url;
}
}
我也有同樣的問題,你提到的,似乎使用上application.properties
指數形式我也不適合我。
爲了解決我不喜歡的東西下面
some.server.url = url1, url2
然後獲得這些屬性的問題,我簡單地使用@Value
@Value("${some.server.url}")
private String[] urls ;
春自動分裂與字符串逗號並返回你一個陣列。 AFAIK這Spring 4+
介紹如果你不想comma (,)
作爲分隔符,你必須使用規劃環境地政司像下面。
@Value("#{'${some.server.url}'.split(',')}")
private List<String> urls;
其中split()
接受分隔符
按照以下步驟
1) @Value( 「$ {} some.server.url」) 私人列表中的URL;
2) @ConfigurationProperties( 「some.server」) 公共類SomeConfiguration {
3) 你應該有實例變量 '網址'
'@Bean \t公開名單的getter和setter someUriList(@Value(「$ {some.server.url}」)列表 urlList)' 返回 'java.lang.IllegalArgumentException:無法解析佔位符'some.server.url'的字符串值「$ {some .server.url}「' –
SebS
Ba在你的問題上,你有屬性爲'some.url'而不是'some.server.url'。這是問題嗎? –
我已經更新了最初的例子,以反映我的application.properties – SebS