Spring .properties文件:将元素作为数组获取 [英] Spring .properties file: get element as an Array
问题描述
我正在使用Spring从.properties
文件中加载属性属性,如下所示:
I'm loading properties attributes from a .properties
file using Spring as follows:
file: elements.properties
base.module.elementToSearch=1
base.module.elementToSearch=2
base.module.elementToSearch=3
base.module.elementToSearch=4
base.module.elementToSearch=5
base.module.elementToSearch=6
spring xml文件
The spring xml file
file: myapplication.xml
<bean id="some"
class="com.some.Class">
<property name="property" value="#{base.module.elementToSearch}" />
</bean>
还有我的Class.java
And my Class.java
file: Class.java
public void setProperty(final List<Integer> elements){
this.elements = elements;
}
但是在调试时,参数元素仅将列表中的最后一个元素获取,因此,存在一个值为"6"的元素的列表,而不是包含6个元素的列表.
But when debugging, the parameter elements only get the last element into the list, so, there is a list of one element with value "6", instead of a list with 6 elements.
我尝试了其他方法,例如仅添加值#{base.module}
,但随后它在属性文件中找不到任何参数.
I tried other approaches, like adding in value only #{base.module}
but then it finds no parameter in the properties file.
一种解决方法是在elements.properties文件中包含一个用逗号分隔的列表,例如:
A workaround is to have in elements.properties file a list separated by commas, like:
base.module.elementToSearch=1,2,3,4,5,6
并将其用作字符串并进行解析,但是有更好的解决方案吗?
and use it as a String and parse it, but is there a better solution?
推荐答案
如果在属性文件中定义数组,例如:
If you define your array in properties file like:
base.module.elementToSearch=1,2,3,4,5,6
您可以像这样在Java类中加载此类数组:
You can load such array in your Java class like this:
@Value("${base.module.elementToSearch}")
private String[] elementToSearch;
这篇关于Spring .properties文件:将元素作为数组获取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!