我想使用带有xml配置的属性文件注入列表值。
<property name="headerOfFile">
<util:list id="headerOfFileList" value-type="java.lang.String">
<value>headerA</value>
<value>headerB</value>
</util:list>
</property>
我想使用xml bean配置通过属性文件注入列表值。 我知道使用Java可以做到这一点:
@Value("#{'${my.list.of.header.strings}'.split(',')}")
private List<String> headerOfFile;
假定使用以下内容正确加载了我的属性文件:
my.list.of.header.strings=headerA,headerB
但是我的要求是使用带有属性文件的xml bean来实现。
答案 0 :(得分:4)
Spring EL也可以使用XML。您所需要做的就是在bean的value
的{{1}}属性中提供相同的表达式。
property
这假设您已启用适当的属性解析配置
<property name="headerOfFile" value="#{'${my.list.of.header.strings}'.split(',')}">
</property>
请注意,表达式中不需要<context:property-placeholder location="your_config.properties" />
<context:annotation-config />
。 Spring已经支持将逗号分隔的值转换为开箱即用的列表/数组。您可以使用
split
您将需要由{p>提供的<property name="headerOfFile" value="${my.list.of.header.strings}">
</property>
DefaultConversionService
答案 1 :(得分:1)
使用setter方法:
private List<String> headerOfFile;
@Value("${my.list.of.header.strings}")
private void setHeaderOfFile(String values) {
this.headerOfFile = Arrays.asList(values.split(','));
}
答案 2 :(得分:1)
如果要将List
的属性注入到特定的bean中,则可以使用@ConfigurationProperties,就像现在使用xml
的配置并将headerOfFileList
注入headerOfFile
豆
@ConfigurationProperties(prefix="my")
public class Config {
private List<String> headerOfFileList = new ArrayList<String>();
public List<String> getServers() {
return this.servers;
}
}
application.properties
my.headerOfFileList[0]=headerA
my.headerOfFileList[1]=headerB
application.yml
my:
headerOfFileList:
- headerA
- headerB