与此问题类似:http://forum.springsource.org/showthread.php?111992-Loading-a-list-from-properties-file-using-Value-annotation(对此没有回复)
我想在.properties文件中有一个值列表,即:
my.list.of.strings=ABC,CDE,EFG
Run Code Online (Sandbox Code Playgroud)
并直接加载到我的班级,即:
@Value("${my.list.of.strings}")
private List<String> myList;
Run Code Online (Sandbox Code Playgroud)
据我所知,这样做的另一种方法是将它放在spring配置文件中,并将其作为bean引用加载(如果我错了,请纠正我),即
<bean name="list">
<list>
<value>ABC</value>
<value>CDE</value>
<value>EFG</value>
</list>
</bean>
Run Code Online (Sandbox Code Playgroud)
但有没有办法做到这一点?使用.properties文件?ps:如果可能的话,我想用任何自定义代码执行此操作.
如何使用Spring中的@Value注释从属性文件中将值注入Map?
我的Spring Java类是和我尝试使用$但是,得到以下错误消息
无法自动装配字段:private java.util.Map Test.standard; 嵌套异常是java.lang.IllegalArgumentException:无法解析字符串值"$ {com.test.standard}"中的占位符'com.test.standard'
@ConfigurationProperty("com.hello.foo")
public class Test {
@Value("${com.test.standard}")
private Map<String,Pattern> standard = new LinkedHashMap<String,Pattern>
private String enabled;
}
Run Code Online (Sandbox Code Playgroud)
我在.properties文件中有以下属性
com.test.standard.name1=Pattern1
com.test.standard.name2=Pattern2
com.test.standard.name3=Pattern3
com.hello.foo.enabled=true
Run Code Online (Sandbox Code Playgroud) java spring annotations dependency-injection spring-annotations