1:使用IDEA的springboot模板创建boot项目
file>new project(new module)>spring initializr

根据需要填写相关信息

选择号需要起步依赖的功能

填写项目位置

项目创建完毕

2:功能介绍
启动类

pom.xml文件

SpringBoot配置文件(application.properties/application.yml)


- yml文件(yaml)的语法
复制代码
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#普通变量 username: zhangsan #体现对象的字段 student: name: zhangsan ager: 18 address: beijing #list/set集合 teacher: - zhangsan - lisi - wanwu #map集合:与“体现对象的字段”的方式一样 #体现list/set集合下的对象字段 teacher02: - name: zhangsan age: 18 address: beijing - name: lisi age: 17 address: tianjin
- 从配置文件中获取值
使用springmvc的@value
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21package com.example.demo.com; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class test { @Value("${student.name}") private String username; @RequestMapping("test01") @ResponseBody public String test(){ System.out.println(username); return username; } }
使用springboot的@ConfigurationProperties(注意:需要配置get/set方法,需要在pom.xml中引入相应的坐标)
复制代码
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
46package com.example.demo.com; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller @ConfigurationProperties(prefix = "student") public class test { private String name; private String age; private String address; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @RequestMapping("test01") @ResponseBody public String test(){ return name+age+address; } }
最后
以上就是想人陪季节最近收集整理的关于springboot 获取yml变量_Spring Boot的快速使用的全部内容,更多相关springboot内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复