SpringBoot读取properties配置⽂件中的数据
Spring Boot最常⽤的3种读取properties配置⽂件中数据的⽅法:
1、使⽤@Value注解读取
读取配置⽂件时,默认读取的是application.properties。
application.properties:
1demo.name=Name
2demo.age=18
代码:
1import org.springframework.beans.factory.annotation.Value;
2import org.springframework.web.bind.annotation.RequestMapping;
3import org.springframework.web.bind.annotation.RestController;
4
5@RestController
6public class GatewayController {
7
8 @Value("${demo.name}")
9 private String name;
10
11 @Value("${demo.age}")
12 private String age;
13
14 @RequestMapping(value = "/gateway")
波罗丁的证物15 public String gateway() {
16 return "get properties value by ''@Value'' :" +
17 //1、使⽤@Value注解读取
18 " name=" + name +
19 " , age=" + age;
20 }
21}
运⾏结果如下:
这⾥,如果要把
@Value("${demo.name}")
private String name;
@Value("${demo.age}")
private String age;
部分放到⼀个单独的类A中进⾏读取,然后在类B中调⽤,则要把类A增加@Component注解,并在类B中使⽤@Autowired⾃动装配类A,代码如下。
类A:
1import org.springframework.beans.factory.annotation.Value;
2import org.springframework.stereotype.Component;
3
4@Component
5public class ConfigBeanValue {
新赛欧颜
6
抗洪救灾标语7 @Value("${demo.name}")
8 public String name;
9
10 @Value("${demo.age}")
11 public String age;
12}qq牧场养什么经验最多
类B:
1import cn.fig.ConfigBeanValue;
2import org.springframework.beans.factory.annotation.Autowired;
3import org.springframework.web.bind.annotation.RequestMapping;
4import org.springframework.web.bind.annotation.RestController;
5
6@RestController
7public class GatewayController {
8
9 @Autowired
10 private ConfigBeanValue configBeanValue;
11
12 @RequestMapping(value = "/gateway")
13 public String gateway() {
14 return "get properties value by ''@Value'' :" +
15 //1、使⽤@Value注解读取
16 " name=" + configBeanValue.name +
17 " , age=" + configBeanValue.age;
18 }
19}
运⾏结果如下:
注意:如果@Value${}所包含的键名在application.properties配置⽂件中不存在的话,会抛出异常:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'configBeanValue': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'demo.name' in value "${demo.na
me}"
2、使⽤Environment读取
application.properties:
1demo.sex=男
2demo.address=⼭东
Java代码:
1import cn.fig.ConfigBeanValue;
2import org.springframework.beans.factory.annotation.Autowired;
3import nv.Environment;
4import org.springframework.web.bind.annotation.RequestMapping;
5import org.springframework.web.bind.annotation.RestController;
6
7@RestController
8public class GatewayController {
9
10 @Autowired
11 private ConfigBeanValue configBeanValue;
12
13 @Autowired
14 private Environment environment;
15
16 @RequestMapping(value = "/gateway")
17 public String gateway() {
18 return "get properties value by ''@Value'' :" +
19 //1、使⽤@Value注解读取
20 " name=" + configBeanValue.name +
21 " , age=" + configBeanValue.age +
22 "<p>get properties value by ''Environment'' :" +
23 //2、使⽤Environment读取
24 " , sex=" + Property("demo.sex") +
25 " , address=" + Property("demo.address");
26 }
27}
运⾏,发现中⽂乱码:
这⾥,我们在application.properties做如下配置:
ding.charset=UTF-8
abled=true
ding.force=true
然后修改IntelliJ IDEA,File --> Settings --> Editor --> File Encodings ,将最下⽅Default encoding for properties files设置为UTF-8,并勾选Transparent native-to-ascii conversion。
重新运⾏结果如下:
3、使⽤@ConfigurationProperties注解读取
在实际项⽬中,当项⽬需要注⼊的变量值很多时,上述所述的两种⽅法⼯作量会变得⽐较⼤,这时候
我们通常使⽤基于类型安全的配置⽅式,将properties属性和⼀个Bean关联在⼀起,即使⽤注解@ConfigurationProperties读取配置⽂件。
在src\main\resources下新建config.properties配置⽂件:
1demo.phone=10086
2demo.wife=self
创建ConfigBeanProp并注⼊config.properties中的值:
1import org.t.properties.ConfigurationProperties;
2import t.annotation.PropertySource;
3import org.springframework.stereotype.Component;
4
5@Component
6@ConfigurationProperties(prefix = "demo")
7@PropertySource(value = "config.properties")
8public class ConfigBeanProp {
9
10 private String phone;
11
12 private String wife;
13
14 public String getPhone() {
15 return phone;
16 }
17
18 public void setPhone(String phone) {
美术学专业19 this.phone = phone;
20 }
21
22 public String getWife() {适合第一场雪发的句子
23 return wife;
24 }
25
26 public void setWife(String wife) {
27 this.wife = wife;
28 }
29}
@Component 表⽰将该类标识为Bean
@ConfigurationProperties(prefix = "demo")⽤于绑定属性,其中prefix表⽰所绑定的属性的前缀。
@PropertySource(value = "config.properties")表⽰配置⽂件路径。
使⽤时,先使⽤@Autowired⾃动装载ConfigBeanProp,然后再进⾏取值,⽰例如下:
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论