一、使用@Value注解读取
读取properties配置文件时,默认读取的是application.properties。
application.properties:
1
2
demo.name = Name
demo.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
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class GatewayController {
@Value("${demo.name}")
private String name;
@Value("${demo.age}")
private String age;
@RequestMapping(value = "/gateway")
public String gateway() {
//1、使用@Value注解读取
return "get properties value by ''@Value'' :" +
" name=" + name +
" , age=" + age;
}
}
这里,如果要把
1
2
3
4
@Value("${demo.name}")
private String name;
@Value("${demo.age}")
private String age;
部分放到一个单独的类A中进行读取,然后在类B中调用,则要把类A增加@Component
注解,并在类B中使用@Autowired
自动装配类A,代码如下:
类A:
1
2
3
4
5
6
7
8
9
10
11
12
13
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class ConfigBeanValue {
@Value("${demo.name}")
public String name;
@Value("${demo.age}")
public String age;
}
类B:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import cn.wbnull.springbootdemo.config.ConfigBeanValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class GatewayController {
@Autowired
private ConfigBeanValue configBeanValue;
@RequestMapping(value = "/gateway")
public String gateway() {
//1、使用@Value注解读取
return "get properties value by ''@Value'' :" +
" name=" + configBeanValue.name +
" , age=" + configBeanValue.age;
}
}
二、使用Environment读取
application.properties:
1
2
demo.sex = 男
demo.address = 山东
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
import cn.wbnull.springbootdemo.config.ConfigBeanValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class GatewayController {
@Autowired
private ConfigBeanValue configBeanValue;
@Autowired
private Environment environment;
@RequestMapping(value = "/gateway")
public String gateway() {
return "get properties value by ''@Value'' :" +
//1、使用@Value注解读取
" name=" + configBeanValue.name +
" , age=" + configBeanValue.age +
"<p>get properties value by ''Environment'' :" +
//2、使用Environment读取
" , sex=" + environment.getProperty("demo.sex") +
" , address=" + environment.getProperty("demo.address");
}
}
运行时如果发现中文乱码,我们在application.properties做如下配置:
1
2
3
4
5
server.tomcat.uri-encoding = UTF-8
spring.http.encoding.charset = UTF-8
spring.http.encoding.enabled = true
spring.http.encoding.force = true
spring.messages.encoding = UTF-8
三、使用@ConfigurationProperties注解读取
在实际项目中,当项目需要注入的变量值很多时,上述所述的两种方法工作量会变得比较大,这时候我们通常使用基于类型安全的配置方式,将properties属性和一个Bean关联在一起,即使用注解@ConfigurationProperties
读取配置文件数据。
在src\main\resources
下新建config.properties配置文件:
1
2
demo.phone = 10086
demo.wife = self
创建ConfigBeanProp并注入config.properties中的值:
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
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "demo")
@PropertySource(value = "config.properties")
public class ConfigBeanProp {
private String phone;
private String wife;
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getWife() {
return wife;
}
public void setWife(String wife) {
this.wife = wife;
}
}
@Component
表示将该类标识为Bean。
@ConfigurationProperties(prefix = "demo")
用于绑定属性,其中prefix表示所绑定的属性的前缀。
@PropertySource(value = "config.properties")
表示配置文件路径。
使用时,先使用@Autowired
自动装载ConfigBeanProp,然后再进行取值,示例如下:
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
31
32
33
34
35
36
import cn.wbnull.springbootdemo.config.ConfigBeanProp;
import cn.wbnull.springbootdemo.config.ConfigBeanValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class GatewayController {
@Autowired
private ConfigBeanValue configBeanValue;
@Autowired
private Environment environment;
@Autowired
private ConfigBeanProp configBeanProp;
@RequestMapping(value = "/gateway")
public String gateway() {
return "get properties value by ''@Value'' :" +
//1、使用@Value注解读取
" name=" + configBeanValue.name +
" , age=" + configBeanValue.age +
"<p>get properties value by ''Environment'' :" +
//2、使用Environment读取
" sex=" + environment.getProperty("demo.sex") +
" , address=" + environment.getProperty("demo.address") +
"<p>get properties value by ''@ConfigurationProperties'' :" +
//3、使用@ConfigurationProperties注解读取
" phone=" + configBeanProp.getPhone() +
" , wife=" + configBeanProp.getWife();
}
}