SpringBoot2学习笔记
源码地址
三、配置文件
3.1)文件类型
3.1.1)properties
同上述的properties用法相同
3.1.2)yaml
3.1.2.1)简介
YAML 是 "YAML Ain't Markup Language"(YAML 不是一种标记语言)的递归缩写。在开发的这种语言时,YAML 的意思其实是:"Yet Another Markup Language"(仍是一种标记语言), 非常适合用来做以数据为中心的配置文件。
3.1.2.2)基本语法
3.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
行内写法: k: [v1,v2,v3]
#或者
k:
- v1
- v2
- v3
3.1.2.4)示例
使用IDEA中的Spring Initailizr创建新工程SpringBootDemo2,新建Bean对象Pet.java,代码如下:
@ToString
@Data
public class Pet {
? ?private String name;
? ?private Double weight;
}
新建Bean对象Person.java,代码如下:
@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;
}
新建Yaml配置文件application.yml给上述对象赋值,代码如下:
person:
# 单引号会将 \n作为字符串输出 ? 双引号会将\n 作为换行输出
# 双引号不会转义,单引号会转义
userName: zhangsan
boss: true
birth: 2019/12/9
age: 18
# interests: [篮球,足球]
interests:
? - 篮球
? - 足球
? - 18
animal: [阿猫,阿狗]
# score:
# ? english: 80
# ? math: 90
score: {english:80,math:90}
salarys:
? - 9999.98
? - 9999.99
?# pet对象内容
pet:
? name: 阿狗
? weight: 99.99
?# 全部pet对象内容
allPets:
? ?# 生病pet对象内容
? sick:
? ? - {name: 阿狗,weight: 99.99}
? ? - name: 阿猫
? ? ? weight: 88.88
? ? - name: 阿虫
? ? ? weight: 77.77
? ?# 健康pet对象内容
? health:
? ? - {name: 阿花,weight: 199.99}
? ? - {name: 阿明,weight: 199.99}
新建控制类HelloController.java用于测试,代码如下:
@RestController
public class HelloController {
? ?@Autowired
? ?Person person;
?
? ?@RequestMapping("/person")
? ?public Person person() {
? ? ? ?String userName = person.getUserName();
? ? ? ?// 输出person对象的UserName属性内容
? ? ? ?System.out.println("UserName = " + userName);
? ? ? ?return person;
? }
}
测试:启动工程,浏览器访问:http://localhost:8080/person,页面如下:

?控制台输出:
2022-06-02 16:32:19.799 INFO 13272 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet ? ? ? : Completed initialization in 1 ms
UserName = zhangsan
添加配置提示:自定义的类和配置文件绑定一般没有提示,为了便于开发添加注释处理器,修改POM.xml,代码如下:
? ? ? ?<dependency>
? ? ? ? ? ?<groupId>org.springframework.boot</groupId>
? ? ? ? ? ?<artifactId>spring-boot-configuration-processor</artifactId>
? ? ? ? ? ?<optional>true</optional>
? ? ? ?</dependency>
?
<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>
|