在本教程中,我们将向您展示如何通过Spring EL @Value
从属性文件导入“列表”
经过测试:
- Spring4.0.6
- JDK 1.7
Spring @Value和列表
在Spring @Value
,您可以使用split()
方法在一行中注入“列表”。
config.properties
server.name=hydra,zeus
server.id=100,102,103
AppConfigTest.java
package com.mkyong.analyzer.test;
import java.util.List;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
@Configuration
@PropertySource(value="classpath:config.properties")
public class AppConfigTest {
@Value("#{'${server.name}'.split(',')}")
private List<String> servers;
@Value("#{'${server.id}'.split(',')}")
private List<Integer> serverId;
//To resolve ${} in @Value
@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
return new PropertySourcesPlaceholderConfigurer();
}
}
输出量
System.out.println(servers.size());
for(String temp : servers){
System.out.println(temp);
}
System.out.println(serverId.size());
for(Integer temp : serverId){
System.out.println(temp);
}
2
hydra
zeus
3
100
102
103
参考文献
翻译自: https://mkyong.com/spring/spring-value-import-a-list-from-properties-file/