同以前的properties用法
YAML 是 “YAML Ain’t Markup Language”(YAML 不是一种标记语言)的递归缩写。在开发的这种语言时,YAML 的意思其实是:“Yet Another Markup Language”(仍是一种标记语言)。
非常适合用来做以数据为中心的配置文件
key: value
行内写法: k: {k1:v1,k2:v2,k3:v3}
#或
k:
k1: v1
k2: v2
k3: v3
行内写法: k: [v1,v2,v3]
#或者
k:
- v1
- v2
- v3
//Person类
@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
//优先在properties文件中进行扫描,扫描完毕后扫描yml文件,如果properties文件中已经配置了的属性在yml文件中不会扫描,即properties文件优先级高于yml文件
@ConfigurationProperties(prefix = "person")
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> salary;
private Map<String, List<Pet>> allPets;
}
//Pet类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Pet {
private String name;
private Double weight;
}
对应的yml(yaml)配置文件
Person:
userName: xiaoming
boss: false
birth: 2022/5/10
age: 18
Pet:
name: xiaohuang
weight: 99.99
interests:
- 篮球
- 足球
# interests:[篮球,足球]
animal: [tiger,bear,bee]
score:
math: 90
english: 88
computer: 100
salary:
- 19999
- 29999
- 39999
allpets:
sick:
- {name: tomcat}
- {name: mouse,weight: 66}
height:
- {name: bird,weight: 30}
- {name: dog,weight: 78}
# height:[{name: bird,weight: 30},{name: dog,weight: 78}]
{"userName":"xiaoming","boss":false,"birth":"2022-05-09T16:00:00.000+00:00","age":18,"pet":{"name":"xiaohuang","weight":99.99},"interests":["篮球","足球"],"animal":["tiger","bear","bee"],"score":{"math":90,"english":88,"computer":100},"salary":[19999.0,29999.0,39999.0],"allPets":{"sick":[{"name":"tomcat","weight":null},{"name":"mouse","weight":66.0}],"height":[{"name":"bird","weight":30.0},{"name":"dog","weight":78.0}]}}
出现问题:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
//表示数据库连接配置出现问题,因为我们在建立项目的时候选择mybatis,但是我们没有配置相应的连接属性,报错
解决方案:
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-validationartifactId>
<version>2.6.6version>
dependency>
相关注解使用:https://www.jianshu.com/p/554533f88370
首先要在实体类使用@Validataed注解,在对应的属性使用相应的注解@Eamil标识该属性只能接受邮箱格式的地址,否则按照message报错
@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
@ConfigurationProperties(prefix = "person")
@Validated
public class Person {
private String userName;
private Boolean boss;
private Date birth;
@Email(message = "邮箱格式错误")
private String email;
private Integer age;
private Pet pet;
private String[] interests;
private List<String> animal;
private Map<String, Object> score;
private Set<Double> salary;
private Map<String, List<Pet>> allPets;
}
email: 2532532
在pom.xml导入下面的依赖,在编写yml配置文件的时候就有提示功能
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-configuration-processorartifactId>
<optional>trueoptional>
dependency>
为了防止将这种功能的jar包也进行打包,我们在pom.xml中添加下列代码可以防止将上述jar包打包
<configuration>
<excludes>
<exclude>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-configuration-processorartifactId>
exclude>
excludes>
configuration>
profile是Spring对不同环境提供不同配置功能的支持,可以通过激活不同的环境版本,实现快速切换环境;
我们在主配置文件编写的时候,文件名可以是 application-{profile}.properties/yml , 用来指定多个环境版本;
例如:
application-test.properties 代表测试环境配置
application-dev.properties 代表开发环境配置
但是Springboot并不会直接启动这些配置文件,它默认使用application.properties主配置文件;
我们需要通过一个配置来选择需要激活的环境:
#比如在配置文件中指定使用dev环境,我们可以通过设置不同的端口号进行测试;
#我们启动SpringBoot,就可以看到已经切换到dev下的配置了;
spring:
profiles:
active: dev
和properties配置文件中一样,但是使用yml去实现不需要创建多个配置文件,更加方便了 !
#多种配置中间用---隔开,使用spring:profiles:active来指定使用某个配置server:
port: 8081
spring:
profiles:
active: dev
---
server:
port: 8082
spring:
profiles: dev
---
server:
port: 8083
spring:
profiles: test
注意:如果yml和properties同时都配置了端口,并且没有激活其他环境 , 默认会使用properties配置文件的!
外部加载配置文件的方式十分多,我们选择最常用的即可,在开发的资源文件中进行配置!
官方外部配置文件说明参考文档
springboot 启动会扫描以下位置的application.properties或者application.yml文件作为Spring boot的默认配置文件:
优先级
1:项目路径下的config文件夹配置文件优先级
2:项目路径下配置文件优先级
3:资源路径下的config文件夹配置文件优先级
4:资源路径下配置文件
优先级由高到底,高优先级的配置会覆盖低优先级的配置;
SpringBoot会从这四个位置全部加载主配置文件;互补配置;
我们在最低级的配置文件中设置一个项目访问路径的配置来测试互补问题;
#配置项目的访问路径
server.servlet.context-path=/xxx