学习笔记--SpringBoot2 配置文件
配置文件
1.1 properties
所有的properties文件参数设置都在application.properties里
1.2 yaml
1.2.1 简介
YAML 是 "YAML Ain't Markup Language"(YAML 不是一种标记语言)的递归缩写。在开发的这种语言时,YAML 的意思其实是:"Yet Another Markup Language"(仍是一种标记语言)。
非常适合用来做以数据为中心的配置文件
1.2.2 基本语法
- key: value;kv之间有空格
- 大小写敏感
- 使用缩进表示层级关系
- 缩进不允许使用tab,只允许空格
- 缩进的空格数不重要,只要相同层级的元素左对齐即可
- '#'表示注释
- 字符串无需加引号,如果要加,''与""表示字符串内容 会被 转义/不转义
1.2.3 数据类型
- 字面量:单个的、不可再分的值。date、boolean、string、number、null
1 | k: v |
- 对象:键值对的集合。map、hash、set、object
1 2 3 4 5 6 | 行内写法: k: {k1:v1,k2:v2,k3:v3} #或 k: k1: v1 k2: v2 k3: v3 |
- 数组:一组按次序排列的值。array、list、queue
1 2 3 4 5 6 | 行内写法: k: [v1,v2,v3] #或者 k: - v1 - v2 - v3 |
示例:
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 | @Data @ConfigurationProperties(prefix = "person") @Component @ToString public class Person { private String userName; private Boolean boss; private Date birth; private Integer age; private Pet pet; private String[] interests; private List< String > animal; private Map< String , Object> score; private Set< Double > salarys; private Map< String , List<Pet>> allPets; } @ToString @Data @NoArgsConstructor @AllArgsConstructor public class Pet { private String name; private Double weight; } |
首先创建实体对象,用lombok将javabean对象的方法编写好,然后用@Component注解将组件放入IOC容器中
创建application.yaml/yml文件,注意:不能修改文件名,不然匹配不了,因为在parent依赖内部有关于配置文件的读取。
yml编写:
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 | # yaml表示以上对象 person: userName: zhangsan boss: false birth: 2019/12/12 20:12:33 age: 18 pet: name: tomcat weight: 23.4 interests: [篮球,游泳] animal: - jerry - mario score: english: first: 30 second: 40 third: 50 math: [131,140,148] chinese: {first: 128,second: 136} salarys: [3999,4999.98,5999.99] allPets: sick: - {name: tom} - {name: jerry,weight: 47} health: [{name: mario,weight: 47}] |
这样容器中的person对象就已经配置了相关数据,只要注入拿来使用就行。
- properties文件和yml文件可以同时生效,但是properties的优先级大于yml文件
- 字符串类型的数据在配置的时候,可以不用写单双引号也能生效
- 单双引号有一个特殊的区别和用法,在面临\n等转义字符的时候,双引号会允许它转义,但是单引号不可以
如:
1 2 3 | username: 'zhangsan \n lisi' 和 username: "zhangsan \n lisi" |
上面单引号时,username还是zhangsan \n lisi
但是双引号会变成 username
lisi
2、配置提示
自定义的类和配置文件绑定一般没有提示。所以为了开发方便,可以加一个依赖,让我们自定义的类和配置文件也能有相关提示。
1 2 3 4 5 | < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-configuration-processor</ artifactId > < optional >true</ optional > </ dependency > |
但是这个只是开发时依赖,打包的时候并不影响我们工程的运行,所以打包可以去掉这一块。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | < build > < plugins > < plugin > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-maven-plugin</ artifactId > < configuration > < excludes > < exclude > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-configuration-processor</ artifactId > </ exclude > </ excludes > </ configuration > </ plugin > </ plugins > </ build > |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器
· 面试官:你是如何进行SQL调优的?