Seb*_*ebS 9 spring spring-mvc spring-boot
我在application.properties文件中有以下内容
some.server.url[0]=http://url
some.server.url[1]=http://otherUrl
Run Code Online (Sandbox Code Playgroud)
如何在@Bean方法中使用@Value anotation引用属性数组?
我正在使用Java 6与Tomcat 7和Spring boot 1.4
Ama*_*har 14
我也遇到了你提到的同样的问题,似乎使用索引表单application.properties也不适用于我.
为了解决这个问题,我做了类似下面的事情
some.server.url = url1, url2
Run Code Online (Sandbox Code Playgroud)
然后得到我简单使用的那些属性 @Value
@Value("${some.server.url}")
private String[] urls ;
Run Code Online (Sandbox Code Playgroud)
Spring会自动用逗号分割 String 并返回一个Array.这是介绍的AFAIKSpring 4+
如果您不想comma (,)作为分隔符,则必须使用如下的SpEL.
@Value("#{'${some.server.url}'.split(',')}")
private List<String> urls;
Run Code Online (Sandbox Code Playgroud)
在那里split()接受分隔符
您可以使用集合.
@Value("${some.server.url}")
private List<String> urls;
Run Code Online (Sandbox Code Playgroud)
您还可以使用配置类并将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;
}
}
Run Code Online (Sandbox Code Playgroud)
按着这些次序
1) @Value("${some.server.url}") 私有列表网址;
2) @ConfigurationProperties("some.server") public class SomeConfiguration {
3)你应该有getter和setter实例变量'urls'
| 归档时间: |
|
| 查看次数: |
19558 次 |
| 最近记录: |