1. 创建person类和Car类,并在person类上加注释 @ConfigurationProperties(prefix = "person"),表明这个类的成员变量的值从配置类注入。注意这里的person类的成员变量需要有get/set方法。
复制代码
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91import org.springframework.boot.context.properties.ConfigurationProperties; import java.util.Date; import java.util.List; import java.util.Map; @ConfigurationProperties(prefix = "person") public class Person { public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } public boolean isMarriage() { return isMarriage; } public void setMarriage(boolean marriage) { isMarriage = marriage; } public Car getCar() { return car; } public void setCar(Car car) { this.car = car; } public List<String> getHobbit() { return hobbit; } public void setHobbit(List<String> hobbit) { this.hobbit = hobbit; } public Map<String, Object> getMaps() { return maps; } public void setMaps(Map<String, Object> maps) { this.maps = maps; } public Date getBirthDate() { return birthDate; } public void setBirthDate(Date birthDate) { this.birthDate = birthDate; } private String name; private Integer age; private double salary; private boolean isMarriage; private Car car; private List<String> hobbit; private Map<String, Object> maps; private Date birthDate; }
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24public class Car { private String carName; private String carBrand; public String getCarName() { return carName; } public void setCarName(String carName) { this.carName = carName; } public String getCarBrand() { return carBrand; } public void setCarBrand(String carBrand) { this.carBrand = carBrand; } }
2. 为person类创建yml配置文件。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18person: name: zhangsan age: 18 salary: 8888.88 car: carName: 奥迪A6L carBrand: 奥迪 hobbit: - 篮球 - rap - 唱歌 - 保健 maps: k1: v1 k2: v2 birthDate: 1991/08/21 marriage: true
3.创建启动类,并加上注释@EnableConfigurationProperties(Person.class),启动的时候提醒Person这个class的成员变量是可以从配置文件注入的。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @EnableConfigurationProperties(Person.class) @RestController public class Tulingspc01SpringbootPropertiesMappingApplication { @Autowired private Person person; public static void main(String[] args) { SpringApplication.run(Tulingspc01SpringbootPropertiesMappingApplication.class, args); } @RequestMapping("/getPersonInfo") public Person getPersonInfo() { return person; } }
测试结果:
最后
以上就是细腻眼睛最近收集整理的关于SpringBoot yml配置文件为变量赋值的全部内容,更多相关SpringBoot内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复