在SpringBoot中注入各种类型(String,List,Map,Object)配置
背景
在开发过程中,绝大多数场景都是要使用一些配置项的。至于配置项如何注入到应用程序中,一直没有过多的关注过。今天抽了点时间实际试了下,这里做个简单的记录,便于需要的朋友了解下。
在下面的讲述中,针对下面的一组配置来进行说明,分别演示读取字符串String、列表List、哈希表Map和对象Object进行说明。开发框架为Springboot。
config:
str: 123456
list:
- 1
- 2
- 3
- 4
- 5
map:
1: first
2: second
3: third
entity:
key: name
value: majing
String类型配置注入
对于String类型的配置,注入最简单,只要简单实用@Value注解就可以了,如下:
@Value("${config.str}")
private String strConfig;
List类型配置注入
对于List类型的配置,注入有两种方式,如下所示。
(1)如果在application.yaml文件中如下配置,
config:
list:
- 1
- 2
- 3
- 4
- 5
那么直接使用@Value注解就可以了,如下所示:
@Value("${config.list}")
private List<Integer> listConfig;
(2)如果在application.yaml文件中不是按照列表格式配置的,
config:
list: 1,2,3,4,5
那么在使用@Value注解时需要注意,使用@Value("${config.list}")没法完成解析,这时候需要做如下修改,调用split()方法对读取的字符串值进行分隔,如果分隔符不一样,那直接替换成其他分隔符就好了。
@Value("#{'${config.list}'.split(',')}")
private List<Integer> listConfig;
Map类型配置注入
maps: "{key1: 'value1', key2: 'value2'}"
punishMap: "{1:7,2:7,3:30,4:30,5:1825}"
注入:
@Value("#{${maps}}")
private Map<String,String> maps;
@Value("#{${punishMap:}}")
private Map<Integer, Integer> punishMap;
注意上面的map解析中,一定要用""把map所对应的value包起来,要不然解析会失败,导致不能转成 Map<String,String>。
实体类注入
@Component
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "config")
public class Config {
private String str;
private List<Object> list;
private Map<String,String> map;
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
public List<Object> getList() {
return list;
}
public void setList(List<Object> list) {
this.list = list;
}
public Map<String, String> getMap() {
return map;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
}
在这个配置实体类中,我们定义了String类型、List类型和Map类型字段,然后在Config类上使用@ConfigurationProperties(prefix = “config”)注解来指出该实体类对应的配置项前缀是application.yaml中config开头的配置项,配置项如下:
config:
str: 123456
list:
- 1
- 2
- 3
- 4
- 5
map: {1: first, 2: second, 3: third}
或者如下配置:
config:
str: 123456
list:
- 1
- 2
- 3
- 4
- 5
map:
1: first
2: second
3: third
需要说明的是List类型必须按照上述配置,否则没法解析。
通过简单的测试接口,我们可以看到配置已经正确的注入了。
Config{str='123456', list=[1, 2, 3, 4, 5], map={1=first, 2=second, 3=third}}
静态属性注入
直接用@Value()的方式不能注入值, 要用set注入
@Component
public class OrderNoUtil {
// B3230651812529
private static String[] yearCodes;
private static Integer beginYear;
@Value("${missyou.year-begin}")
private void setBeginYear(Integer beginYear) {
OrderNoUtil.beginYear = beginYear;
}
@Value("${missyou.year-codes}")
public void setYearCodes(String yearCodes) {
OrderNoUtil.yearCodes = yearCodes.split(",");
}
public static String makeOrderNo() {
StringBuilder joiner = new StringBuilder();
Calendar calendar = Calendar.getInstance();
String mills = String.valueOf(calendar.getTimeInMillis());
String micro = LocalDateTime.now().toString();
String random = String.valueOf(Math.random() * 1000).substring(0, 2);
joiner.append(OrderNoUtil.yearCodes[calendar.get(Calendar.YEAR) - OrderNoUtil.beginYear])
.append(Integer.toHexString(calendar.get(Calendar.MONTH) + 1).toUpperCase())
.append(calendar.get(Calendar.DAY_OF_MONTH))
.append(mills.substring(mills.length() - 5))
.append(micro.substring(micro.length() - 3))
.append(random);
return joiner.toString();
}
}
网友评论