网站首页> 文章专栏> SpringBoot配置-yml配置与实体类绑定,获取yml中配置的值
SpringBoot配置-yml配置与实体类绑定,获取yml中配置的值
日期:2021-11-19 09:00:19 作者:管理员 浏览量:2803

1.新建一个springboot项目

2.引入相关依赖

 1     <dependencies>
 2         <dependency>
 3             <groupId>org.springframework.boot</groupId>
 4             <artifactId>spring-boot-starter-web</artifactId>
 5         </dependency>
 6 
 7         <!--编写测试代码的依赖-->
 8         <dependency>
 9             <groupId>org.springframework.boot</groupId>
10             <artifactId>spring-boot-starter-test</artifactId>
11             <scope>test</scope>
12         </dependency>
13 
14         <!--和yml配置文件绑定所需要的依赖-->
15         <dependency>
16             <groupId>org.springframework.boot</groupId>
17             <artifactId>spring-boot-configuration-processor</artifactId>
18             <optional>true</optional>
19         </dependency>
20     </dependencies>

3.编写实体类,实体类中添加get,set,toString方法

 1 package com.xiaoma.ymlvalueobtain.bean;
 2 
 3 
 4 public class Dog {
 5     private String DogName;
 6     private Integer DogAge;
 7 
 8     public String getDogName() {
 9         return DogName;
10     }
11 
12     public void setDogName(String dogName) {
13         DogName = dogName;
14     }
15 
16     public Integer getDogAge() {
17         return DogAge;
18     }
19 
20     public void setDogAge(Integer dogAge) {
21         DogAge = dogAge;
22     }
23 
24     @Override
25     public String toString() {
26         return "Dog{" +
27                 "DogName='" + DogName + '\'' +
28                 ", DogAge='" + DogAge + '\'' +
29                 '}';
30     }
31 }
 1 package com.xiaoma.ymlvalueobtain.bean;
 2 
 3 import org.springframework.boot.context.properties.ConfigurationProperties;
 4 import org.springframework.stereotype.Component;
 5 
 6 import java.util.Date;
 7 import java.util.List;
 8 import java.util.Map;
 9 
10 public class Person {
11     private String PersonName;
12     private Integer PersonAge;
13     private boolean Boos;
14     private Date PersonBrith;
15 
16     private Map<String,Object> map;
17     private List<Object> list;
18     private Dog dog;
19 
20     public String getPersonName() {
21         return PersonName;
22     }
23 
24     public void setPersonName(String personName) {
25         PersonName = personName;
26     }
27 
28     public Integer getPersonAge() {
29         return PersonAge;
30     }
31 
32     public void setPersonAge(Integer personAge) {
33         PersonAge = personAge;
34     }
35 
36     public boolean isBoos() {
37         return Boos;
38     }
39 
40     public void setBoos(boolean boos) {
41         Boos = boos;
42     }
43 
44     public Date getPersonBrith() {
45         return PersonBrith;
46     }
47 
48     public void setPersonBrith(Date personBrith) {
49         PersonBrith = personBrith;
50     }
51 
52     public Map<String, Object> getMap() {
53         return map;
54     }
55 
56     public void setMap(Map<String, Object> map) {
57         this.map = map;
58     }
59 
60     public List<Object> getList() {
61         return list;
62     }
63 
64     public void setList(List<Object> list) {
65         this.list = list;
66     }
67 
68     public Dog getDog() {
69         return dog;
70     }
71 
72     public void setDog(Dog dog) {
73         this.dog = dog;
74     }
75 
76     @Override
77     public String toString() {
78         return "Person{" +
79                 "PersonName='" + PersonName + '\'' +
80                 ", PersonAge=" + PersonAge +
81                 ", Boos=" + Boos +
82                 ", PersonBrith=" + PersonBrith +
83                 ", map=" + map +
84                 ", list=" + list +
85                 ", dog=" + dog +
86                 '}';
87     }
88 }

4.右键resources文件夹,选择new-》file,文件名字为application.yml,然后编写yml配置文件数据(注意:编写yml配置数据的时候要注意空格)

 1 person:
 2   PersonName: xiaoma
 3   PersonAge: 18
 4   Boos: true
 5   PersonBrith: 1999/11/12
 6   map: {k1: v1,k2: 12}
 7   list:
 8     - zhangsan
 9     - lisi
10   dog:
11     DogName: 小狗
12     DogAge: 10

5.通过再Person类里的类名上添加@Component和@ConfigurationProperties(prefix = "person")注解来将配置文件数据和实体类进行绑定,其中prefix就是yml配置文件中要绑定的东西。因为只有当@ConfigurationProperties是容器中的组件的时候,才能使用容器提供的功能,他才会起作用,所以我们要把这个这个注解通过@Component注解加载到容器中

 6.进行单元测试

1     @Autowired      //将Person类注入进来  
2     Person person;
3     @Test
4     public void test01(){
5         System.out.println(person);
6     }

7.运行结果,就这样就获取到了yml文件中的值

补充:@Value和@ConfigurationProperties注解的区别

 如果只需要获取配置文件中的某一个值的时候时候@value,如果我们要获取配置文件中的所有值用@ConfigurationProperties

来说两句吧
最新评论
    热门文章