我是靠谱客的博主 粗犷手链,这篇文章主要介绍【系统学习SpringBoot】SpringBoot读取Property配置,现在分享给大家,希望可以做个参考。



SpringBoot读取Property

在web开发的过程中,经常需要自定义一些配置文件作为全局配置(数据库连接之类的)。
在SpringBoot中,读取property文件会很简单。

真的很简单,,不信看下面 ▼.▼

【1】在application.yml中添加追加如下配置(yml格式)

复制代码
1
2
3
4
5
6
#yml语法比起 properties更加方便,,, xatu: zsl: name: 小鼠标 age: 21 id: 10086

【2】编写获取配置信息的类

复制代码
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
package xatu.zsl.domain; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; /** * Created by zsl on 2017/9/4. */ @Component public class XatuZSLinfo { @Value("${xatu.zsl.name}") private String name; @Value("${xatu.zsl.id}") private String id; @Value("${xatu.zsl.age}") private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getId() { return id; } public void setId(String id) { this.id = id; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }

【3】搞个测试方法,试试

复制代码
1
2
3
4
5
6
7
8
@Autowired private XatuZSLinfo properties; @GetMapping("/getzsl") public XatuZSLinfo getXatuZSLinfo() { return properties; }

这里写图片描述

最后

以上就是粗犷手链最近收集整理的关于【系统学习SpringBoot】SpringBoot读取Property配置的全部内容,更多相关【系统学习SpringBoot】SpringBoot读取Property配置内容请搜索靠谱客的其他文章。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部