文章目录
一、yaml
1、yaml介绍
YAML 是 “YAML Ain’t a Markup Language”(YAML 不是一种标记语言)的递归缩写。在开发的这种语言时,YAML 的意思其实是:“Yet Another Markup Language”(仍是一种标记语言)。
YAML 的语法和其他高级语言类似,并且可以简单表达清单、散列表,标量等数据形态
。它使用空白符号缩进和大量依赖外观的特色
,特别适合用来表达或编辑数据结构、各种配置文件、倾印调试内容、文件大纲。
yaml文件的格式为xxx.yaml
2、yaml基本语法
①、基本要求
- 大小写
敏感
- 使用
缩进
表示层级关系 - 缩进
不允许
使用tab,只允许空格 - 缩进的空格数不重要,只要
相同层级的元素左对齐
即可 - '#'表示注释
YAML 支持以下几种数据类型:
- 对象:键值对的集合,又称为映射(mapping)/ 哈希(hashes) / 字典(dictionary)
- 数组:一组按次序排列的值,又称为序列(sequence) / 列表(list)
- 纯量(scalars):单个的、不可再分的值
②、yaml键值对
在properties中我们要是表示key-value,那么应该如下的格式
name=hanjiang
在yaml中表示key-value,如下(: 后面一定要有"空格")
普通表示:
name: hanjiang
③、yaml对象
在properties中我们要是表示一个对象,格式如下:
person.name=hanjiang
person.age=18
person.gender=man
在yaml中表示对象有两种格式,如下
普通表示:
person:
name: hanjiang
age: 18
gender: man
行内表示:
person: {name: hanjiang, age: 18, gender: man}
④、yaml数组
以 - 开头的行表示构成一个数组:
nums:
- 10
- 20
- 30
行内表示:
nums: [10, 20, 30]
⑤、yaml纯量
纯量是最基本的,不可再分的值,包括:
- 字符串
- 布尔值
- 整数
- 浮点数
- Null
- 时间
- 日期
使用一个例子来快速了解纯量的基本使用:
boolean:
- TRUE #true,True都可以
- FALSE #false,False都可以
float:
- 3.14
- 6.8523015e+5 #可以使用科学计数法
int:
- 123
- 0b1010_0111_0100_1010_1110 #二进制表示
null:
nodeName: 'node'
parent: ~ #使用~表示null
string:
- 哈哈
- 'Hello world' #可以使用双引号或者单引号包裹特殊字符
- newline
newline2 #字符串可以拆成多行,每一行会被转化成一个空格
date:
- 2018-02-17 #日期必须使用ISO 8601格式,即yyyy-MM-dd
datetime:
- 2018-02-17T15:02:31+08:00 #时间使用ISO 8601格式,时间和日期之间使用T连接,最后使用+代表时区
⑥、yaml中的SPEL表达式
随机数:
# 取int类型随机数
random-int: ${random.int}
# 取一定范围内的int类型随机数
random-int-range: ${random.int(beg, end)}
# 取long类型的随机数
random-long: ${random.long}
# 取一定范围内的long类型随机数
random-long-range: ${random.long(beg, end)}
# 取UUID
random-uuid: ${random.uuid}
取同文件中的其他值:
pserson:
last-name: 寒江
first-name: coder
all-name: ${person.last-name}${person.first-name}
二、通过配置文件(yaml/properties)给实体类的属性赋值
在spring阶段,我们给实体类赋值是通过< bean>实现的,到了springboot阶段我们可以通过配置文件的形式给实体类进行赋值,推荐使用yaml文件进行属性赋值
。
1.、普通绑定(yaml/properties)
我们首先需要一个实体类Person
@ConfigurationProperties(prefix = “person”):绑定yaml或者properties中的person对象,如果application.yaml和application.properties同时存在,那么将会优先绑定application.properties
。
package com.example.demo.pojo;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
private String name;
private int age;
public Person() {
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
编写一个application.yaml
person:
name: 寒江
age: 18
或者编写一个application.properties
person.name=寒江
person.age=18
2、松散绑定(yaml)
松散绑定只能在yaml的配置文件
中使用
对于实体类Person的属性,我们如果将name改为lastName
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
private String lastName;
...
}
对应的yaml配置文件中应该为
person:
last-name: 寒江
...
综上所述,yaml的优点要大于传统的properties,所以spring官方推荐使用yaml
你可能会遇到如下的提示
解决方法:
pom.xml中添加
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
三、JSR303校验
我们都知道,在html中的input中是有校验功能的,例如< intpt type=“email”>中只能写email,如果写的不是email那么就会提示。
在springboot中也有类似的功能,我们用用一个实例进行说明
第一步:我们需要使用一个启动器(pom.xml)
<!--将 Java Bean 验证与 Hibernate Validator 结合使用的入门工具-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
第二步:给相应的实体类加上@Validated注解
@Component
@ConfigurationProperties(prefix = "person")
@Validated
public class Person {
private String lastName;
...
}
第三步:给相应的属性加上验证注解(以email验证为例)
@Component
@ConfigurationProperties(prefix = "person")
@Validated
public class Person {
@Email(message = "name变量未正确填写email类型的数据!")
private String lastName;
...
}
第四步:启动测试
因为name属性的值不email格式的数据
第五步:修改name的数据格式
person:
name: "1000000@qq.com"
age: 18
第六步:测试成功
常使用的注解
四、配置文件位置
在springboot中的配置文件的位置不是只能写在classPath(resources目录)下面的。
spring官方给出了四个位置:
file:/config/:等价于"项目名/config
"
file:./:等价于"项目名/
"
classPath:/config/:等价于"resources/config
"
classPath:./:等价于"resources/
"
下面,我们将对上述的4个位置的优先级进行讨论
第一步:我们先在上述的四个位置创建好application.propperties/aapplication.yaml文件
第二步:启动测试
四个文件都保留时:
保留后三个文件时:
保留后两个文件时:
保留最后一个文件时:
结论:
file:/config/优先级大于file:./优先级大于classPath:/config/优先级大于classPath:./
五、多环境配置
在开发中,我们通常会有开发环境、测试环境和生产环境,通常三者使用的数据库是不一样的,这就需要我们要具有切换环境的能力。
1、通过properties文件实现多环境配置
application-test.properties:测试环境的配置文件
application-dev.properties:开发环境的配置文件
application-test.properties文件(测试环境)
...
server.port=8086
...
application-dev.properties文件(开发环境)
...
server.port=8085
...
当application.properties文件配置如下是
...
#springboot的多环境配置,可以选择激活那个环境
spring.profiles.active=test
...
启动
当application.properties文件配置如下是
...
#springboot的多环境配置,可以选择激活那个环境
spring.profiles.active=dev
...
启动
2、通过yaml文件实现多环境配置(推荐)
使用开发环境:
#公共配置
person:
name: "1000000@qq.com"
age: 18
server:
port: 8080
spring:
profiles:
active: dev #使用名为dev的配置
---
# 开发环境
spring:
profiles: dev
server:
port: 8087
---
# 测试环境
spring:
profiles: test
server:
port: 8088
---
# 生产环境
spring:
profiles: pro
server:
port: 8089
启动:
使用测试环境:
...
spring:
profiles:
active: test #使用名为test的配置
...
启动:
使用生产环境:
...
spring:
profiles:
active: pro #使用名为pro的配置
...
启动:
六、application.yaml中可配置的内容
我们知道application.yaml是springboot的核心配置文件文件,但是对于springboot中到低可以写一些什么东西,我们现在还是很模糊的,加下来我们将一起其探究springboot的核心配置we你按中到能够配置什么样的东西。
1、官方文档
2、探究原理、找寻规律
在"自动装配原理"那一节的博客中,我们知道有一个很重的文件"spring.factories",打开"spring.factories"文件之后,我们可以发现其中又很多的自动配置类,例如
我们随便点开一个(org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration)。点来之后,发现有这个几个比较重要的注解
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass({ RabbitTemplate.class, Channel.class })
@EnableConfigurationProperties(RabbitProperties.class)
@Import(RabbitAnnotationDrivenConfiguration.class)
其中EnableConfigurationProperties(RabbitProperties.class)中的(RabbitProperties.class)与RabbitAutoConfiguration的配置属性貌似有关联。
点开RabbitProperties.class之后,其中有一个很熟悉的注解@ConfigurationProperties(prefix = “spring.rabbitmq”)
@ConfigurationProperties(prefix = “spring.rabbitmq”):加载application.yaml中的spring.rabbitmq下的配置,并给该类中的属性进行赋值。
然后我就在application.yaml尝试的输入了"spring.rabbitmq",结果如下图所示
在RabbitProperties.class中我们也可以发现有与之对应的host属性,如下:
就此我们貌似的找到了"application.yaml文件"中可配置的内容"的规律:
- 对于
spring.factories文件
中存在的XxxxAutoConfiguration自动配置类
,点开该类之后都会存在@EnableConfigurationProperties注解
。 - 对于存在参数
XxxxProperties.class
的@EnableConfigurationProperties注解,点开XxxxProperties.class类后,XxxxProperties.class类中总会存在@ConfigurationProperties(prefix = "spring.Xxxx")
。 - 我们可以在
application.yaml
中通过spring.Xxxx.
进行配置XxxxProperties.class类中的属性值。 - 或者说,只要你可以找到
prefix = "spring.Xxxx"
,即使不是在@ConfigurationProperties中要到的那么也是可以在application.yaml中进行配置的,例如:
3、查看未生效的配置类
对于众多的自动配置类,并不是所有的配置类都会生效
。对于未生效的自动配置类,我们只需要在pom.xml
添加中添加对应的启动器
即可。
查看哪些自动配置类生效,哪些未生效,我们需要在application.yaml中配置如下配置:
# 可以查看那些自动配置类生效(Positive matches),那些没有生效(Negative matches)
debug: true
启动主程序:
4、自动装配的原理:
1、SpringBoot在启动时会加载大量的自动配置类
2.、我们需要的功能要是在SpringBoot默认写好的自动配置类中,且我们需要的组件存在其中,那么我们就可以直接使用,不需要在手动配置
3.、在给容器中自动配置类添加组件的时候,会从XxxProperties类中获取某些属性,我们只需在配置文件(application.yaml)中指定属性值即可
5、XxxxAutoConfiguration:自动配置类,给容器中添加组件
6、XxxxProperties:封装配置文件中相关的属性
生活没有你想象的那么美好,但是也没有你想象的那么糟糕…