Spring屬性注入Bean的List屬性

使用@Value和SpEL來完成。spring

屬性文件:spa

client.valid.version=1.2,1.1

XML配置:it

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
 <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
 <property name="ignoreResourceNotFound" value="false" />
 <property name="fileEncoding" value="UTF-8" />
 <property name="locations">
 <list>
 <value>classpath*:/config/dev/*.properties</value>
 </list>
 </property>
 </bean>

注入Bean:io

@Value("#{'${client.valid.version}'.split(',')}")
 private List<String> validVersions;
 public boolean isVersionValid(String version) {
 if (StringUtils.isBlank(version)) {
 return false;
 }
 return validVersions.contains(version);
 }

SpEL格式爲#{},表達式內部運算轉換成List。class

相關文章
相關標籤/搜索