配置文件我们在之前用properties作为配置文件,SpringBoot还兼容另外一种配置文件格式-yaml
只要有全局配置文件,不管是appilcation.properties还是application.yml,哪怕两个都有,都会生效,这两个合并起来一起生效
1.文件类型
1.1 properties
同以前的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,只允许空格(idea中无所谓)
- 缩进的空格数不重要,只要相同层级的元素左对齐即可
- '#'表示注释
- 字符串无需加引号,如果要加,''与""表示字符串内容 会被 转义/不转义
1.2.3 数据类型
- 字面量:单个的、不可再分的值。date、boolean、string、number、null
k: v
- 对象:键值对的集合。map、hash、set、object
因为对象里面都有属性
两种写法:
-
- 行内写法
k: {k1:v1,k2:v2,k3:v3}
-
- 对象表示法
k: k1: v1 k2: v2 k3: v3
- 数组:一组按次序排列的值。array、list、queue
案例:
Person.java
package com.huawei.boot.bean; import lombok.Data; import lombok.ToString; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import java.util.Date; import java.util.List; import java.util.Map; import java.util.Set; @ConfigurationProperties(prefix = "person") @Component // 在类上加上该注解,代表是容器中的组件了 @ToString @Data 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; }
Pet.java
@ToString @Data public class Pet { private String name; private Double weight; }
HelloController.java
package com.huawei.boot.Controller; import com.huawei.boot.bean.Person; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @Autowired Person person; @RequestMapping("/person") public Person person () { return person; } }
Boot01Helloworld2Application.java
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Boot01Helloworld2Application { public static void main(String[] args) { SpringApplication.run(Boot01Helloworld2Application.class, args); } }
application.yml
person:
userName: zhangsan
boss: true
birth: 2019/12/9
age: 19
# interests: [篮球,足球]
interests:
- 篮球
- 羽毛球
- 橄榄球
animal: [阿猫, 阿狗]
score:
english: 80
math: 90
salarys:
- 999.98
- 999.99
pet: {name: 阿狗, weight: 99.89}
allPets:
sick:
- {name: tom, weight: 44.22}
- {name: jerry, weight:33.33}
- name: 啊猫
weight: 92.33
- name: 啊虫
weight: 77.77
health:
- {name: 阿花, weight: 199.55}
- {name: 啊明, weight: 111.11}
启动服务器后,浏览器中:
通过@ConfigurationProperties(prefix = "person")可以将Person类与application.yml绑定起来
@ConfigurationProperties的作用:
将配置文件中配置的每一个属性的值,映射到这个组件中,告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定。
参数prefix="person":将配置文件中的person下面的所有属性一一对应
只有这个组件是容器中的组件,才能使用容器提供的@ConfigurationProperties功能,因此要加上@Component,表明注册bean
2.自定义类绑定的配置提示
自定义的类和配置文件绑定一般没有提示,下面实现在yml中写的时候,能自动提示
在pom.xml中加入
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
重启一下
在application.yml中,写的时候,就会有提示了
在开发期间,我们有提示了,但是在开发完成后,在我们SpringBoot的打包插件中重新打包的时候,不要把配置处理器打包到jar包中
再在pom.xml中添加:
<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>
加载指定的配置文件,即文件名不是application.properties可以用下面的方法
在application.yml中使用随机符号
注意:
配置文件如果用application.properties在写中文的时候,会有乱码,我们需要去IEDA中设置编码格式为UTF-8