从属性文件中读取List并使用spring注释加载@Value [英] Reading a List from properties file and load with spring annotation @Value
问题描述
与此问题类似: http://forum.springsource.org/showthread.php?111992-Loading-a-list-from-properties-file-using-Value-annotation (对此没有回复)
Similar to this question: http://forum.springsource.org/showthread.php?111992-Loading-a-list-from-properties-file-using-Value-annotation (for which there's no reply on)
我想在.properties文件中有一个值列表,即:
I want to have a list of values in a .properties file, ie:
my.list.of.strings=ABC,CDE,EFG
并加载它我的班级直接,即:
And to load it in my class directly, ie:
@Value("${my.list.of.strings}")
private List<String> myList;
据我所知,这样做的另一种方法是将它放在spring配置文件中,并加载它作为一个bean引用(如果我错了就纠正我),即
As I understand, an alternative of doing this is to have it in the spring config file, and load it as a bean reference (correct me if I'm wrong), ie
<bean name="list">
<list>
<value>ABC</value>
<value>CDE</value>
<value>EFG</value>
</list>
</bean>
但有没有办法做到这一点?使用.properties文件?
ps:如果可能,我想用任何自定义代码执行此操作。
But is there any way of doing this? using a .properties file? ps: I would like to do this with out any custom code if possible.
推荐答案
使用Spring EL:
Using Spring EL:
@Value("#{'${my.list.of.strings}'.split(',')}")
private List<String> myList;
假设您的属性文件已正确加载以下内容:
Assuming your properties file is loaded correctly with the following:
my.list.of.strings=ABC,CDE,EFG
这篇关于从属性文件中读取List并使用spring注释加载@Value的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!