我是靠谱客的博主 想人陪季节,这篇文章主要介绍springboot 获取yml变量_Spring Boot的快速使用,现在分享给大家,希望可以做个参考。

1:使用IDEA的springboot模板创建boot项目

file>new project(new module)>spring initializr

fbedc74c21628c04e0ec02f4465cdc99.png
IDEA需要启用springboot插件才会显示这个组件

根据需要填写相关信息

4e8fc0d3583619f9f58c8118f551eb74.png

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

87fe8a6064526055533114b9ade300f4.png

填写项目位置

8b4cde29df642aeda4dfab25ae284c7f.png

项目创建完毕

1d323f6c9d6ec9c4cc589fd360e60844.png

2:功能介绍

启动类

5fbb120a39c9c2088ba83ca2897f6e2a.png

pom.xml文件

66fa4241b4bd858972a461fa15bb9e39.png

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

421da4884c72f07642ab77dab0122f1e.png

764dff015361ccf1b7548d35a45ec618.png
  • 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
21
package 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
46
package 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内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(88)

评论列表共有 0 条评论

立即
投稿
返回
顶部