Springboot @Value读取map或list的prop
2019-07-28 本文已影响0人
依然慢节奏
一、properties文件内容
config-prod.properties
属性配置文件:
yjs.client.functional.filter=A,B,C,D
yjs.client.functional.map={key:"value"}
二、属性文件加载配置
PropertyProductConfig
配置文件加载类
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
/**
* Email: love1208tt@foxmail.com
* Copyright (c) 2019. missbe
* @author lyg 19-7-10 下午7:48
*
*
**/
@Profile({"prod","test"})
@Configuration
@PropertySource(value = "classpath:conf/config-prod.properties",ignoreResourceNotFound = true,encoding = "UTF-8")
public class PropertyProductConfig {
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer(){
return new PropertySourcesPlaceholderConfigurer();
}
}
三、属性对应实体类
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
@SuppressWarnings("all")
public class ContainerProperties {
private static final Logger LOGGER = LoggerFactory.getLogger(ContainerProperties.class);
@Value(value = "#{'${yjs.client.functional.filter}'.split(',')}")
private List<String> FUNCTION_FILTER;
@Value("#{${yjs.client.functional.map}}")
private Map<String, String> maps;
public List<String> getFUNCTION_FILTER() {
return FUNCTION_FILTER;
}
public void setFUNCTION_FILTER(List<String> FUNCTION_FILTER) {
this.FUNCTION_FILTER = FUNCTION_FILTER;
}
public Map<String, String> getMaps() {
return maps;
}
public void setMaps(Map<String, String> maps) {
this.maps = maps;
}
}