SpringBoot读取配置

By youfang

读取配置

在application.properties写入下面代码

1
2
3
4
5
6
7
8
9
10
test.boolean=true
test.string=abc
test.integer=123
test.long=123
test.float=1.2345678123456
test.double=1.2345678123456
test.array=1,3,4,5,6,1,2,3
test.list=1,3,4,5,6,1,2,3
test.set=1,3,4,5,6,1,2,3
test.map={name:"张三", age:18}

Java中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
@Value("${test.boolean}")
private Boolean testBoolean;

@Value("${test.string}")
private String testString;

@Value("${test.integer}")
private Integer testInteger;

@Value("${test.long}")
private Long testLong;

@Value("${test.float}")
private Float testFloat;

@Value("${test.double}")
private Double testDouble;

@Value("#{'${test.array}'.split(',')}")
private String[] testArray;

@Value("#{'${test.list}'.split(',')}")
private List<String> testList;

@Value("#{'${test.set}'.split(',')}")
private Set<String> testSet;

@Value("#{${test.map}}")
private Map<String, Object> testMap;

使用@Value注解,如果配置文件中没有设置则在启动的时候会报错。
可以通过@Value("${xxx:192.168.0.1}"),空字符串:@Value("${v.name:}")设置默认值。

Java中为static属性赋值

1
2
3
4
5
6
public static String UPLOAD_MODE = null;

@Value("${upload.mode}")
public void setUploadMode(String uploadMode) {
UPLOAD_MODE = uploadMode;
}