一. 给实体类对象赋值
1.yaml配置文件中的属性可以批量注入到实体类文件中,与其中的对象一一对应
2.松散绑定
(1)何为松散绑定?
命名不太规范,包括大小写等,都不会影响赋值,这就是松耦合
yaml文件下命名
class文件下命名
first_name firstName (2)绑定赋值
class @Component //组件 @ConfigurationProperties(prefix = "xxx") //与yaml配置文件内容绑定 public class pet { private String name; }
yaml xxx: name: hello
test @Autowiredprivent pet abc; @Test void contextLoads() { System.out.println(abc); }
添加@Component的原因时需要通过Spring容器技术(Bean)获取对象
添加@ConfigurationProperties(prefix = "xxx"),将配置文件加载到class文件中,实现赋值class文件中定义私有字符串属性name,在yaml配置文件中给name赋值为hello,到Test类中打印输出,查看是否赋值。
3.JSR303数据校验
JSR303是一套JavaBean参数校验的标准,可以直接将这些注解加在JavaBean的属性上面,就可以在需要校验的时候进行校验了
二. 可以创建
例:
#修改端口 | #对象的行内写法 | #数组行内写法 |
server: port: 8081 | student: {name: 董文杰,age: 20} | pets: [cat,dog,tiger] |
三. 多环境切换
修改active参数
server:
port: 8081
spring:
profiles:
active: test
---
server:
port: 8082
spring:
profiles: dev
---
server:
port: 8083
spring:
profiles: test
由第一个 spring:profiles:active: 决定使用哪个环境active:后面是dev就使用dev生产环境,后面是test就使用测试环境