Spring Boot 学习摘要--关于配置

Spring Boot 学习摘要–关于配置

学习教程来自:B站 尚硅谷

1. 关于配置

1. Yaml

  1. 键值对写法:必须要有空格!

    v```
    1
    2
    3
    2. v 的一些写法

    - v 是对象

    friends:

    name: zhangsan
    age: 20

    行内写法:
    friends: {name: zhangsan, age: 20}

    1
    - v 是数组

    pets:

    - cat
    - dog

    行内写法:
    pets: [cat,dog]

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18

    #### 2. properties 文件

    默认编码是 utf-8 编码,在获取其中的中文时可能有问题,需要修改一下设置

    在 settings -> File Encodings 中勾选 “Transparent native-to-ascii conversion”

    #### 3. 获取配置中的值

    - `@ConfigurationProperties`

    `@ConfigurationProperties(prefix = "?")` 告诉 SpringBoot 将本类中的所有属性和篇日志文件中相关的配置进行绑定

    参数 prefix 指定配置文件中某一个 key 下的所有属性进行一一映射

    - `@Value`

    `@Value` 使用 $ 来获取配置中的值,或者使用 # 来实时计算

    @Value(“${person.last-name}”)
    private String name;
    @Value(“#{11*2}”)
    private int age;

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20

    xxx|@ConfigurationProperties|@Value
    :-:|:-:|:-:|
    功能|批量注入配置文件中的属性|一个个指定
    松散绑定|支持|不支持
    SpEL|不支持|支持
    JSR303数据校验|支持|不支持
    复杂类型封装 eg:map|支持|不支持

    * 松散绑定:lastName / last-name / last_name 都可以识别成 lastName
    * SpEL:#{11*2} 这种方式
    * JSR303数据校验:在类上加入注解 `@Validated`,在变量前加入注解比如 `@Email`,可以自动识别是否符合邮箱格式

    **如果只是简单获取一下配置的值,就用@Value**

    #### 4. @PropertySource 和 @ImportResource

    - @PropertySource:加载指定的配置文件,在配置类上添加

    ```@PropertySource(value={"classpath:person.properties"})
  • @ImportResource:导入指定的配置文件,在启动类上添加
1
2

#### 5. 配置文件占位符

#name: zhangsan
age: ${random.int} # 随机数
pet: ${name:zs}_dog # 获取前面 key 为 name 的值,如果没有,用默认值 zs 代替

1
2
3
4
5
6
7
8
9

#### 6. 测试、生产多配置文件指定

1. 使用 properties 文件,多 profiles 的形式
application.properties: 主配置文件
application-dev.properties: 测试配置文件
application-prod.properties: 生产配置文件

默认启动的时候会激活主配置文件,切换配置文件时,在主配置文件中添加

spring.profiles.active=dev/prod

1
2
3
4

2. 使用 yml 文件,多文档块的形式

appication.yml 使用 --- 三个横线可以在一个文件中添加多个文档块,相当于编写了好几个文档

server:
port: 8081
spring:
profiles:

active:dev

server:
port: 8082
spring:
profiles: dev


server:
port: 8083
spring:
profiles: prod

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

#### 7. 配置文件目录

- file:/config/ -> 项目根目录/config/
- file:/ ->项目根目录
- classpath:/config/ -> resources/config/
- classpath:/ -> resources

优先级由高到低;相同配置项以高优先级的配置为主;互补配置(会全部加载)

#### 8. 自动配置原理

Spring Boot 启动时加载主配置类,开启自动配置功能 `@EnableAutoConfiguration`

`@EnableAutoConfiguration` 的作用:

1. 利用 `AutoConfigurationImportSelector.class` 给容器导入一些组件

2. 通过 `selectImports()` 方法来确定哪一些配置被导入

List configurations = this.getCandidateConfigurations(annotationMetadata, attributes);

protected List getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
// 扫描所有 jar 包类路径下的 META-INF/spring.factories
// 把扫描到的文件内容包装成properties对象
// 从properties中获取到EnableAutoConfiguration.class类对应的值,添加到容器中
List configurations = SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader());
Assert.notEmpty(configurations, “No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make sure that file is correct.”);
return configurations;
}


=> 

将类路径 `META-INF/spring.factories` 下配置的所有 `EnableAutoConfiguration.class` 的值加入到容器中

eg: `xxxAutoConfiguration.class` 每一个这样的类都是容器中的一个组件,加入到容器后,再用他们来做自动配置

能配置的属性都来源于 `xxxAutoConfiguration.class` 类对应的 `xxxProperties.class` 类

在 `xxxProperties.class` 类前会有很多注解,比如 `@ConditionalOnWebApplication` 用来判断是否是一个web应用,如果是返回true,下面的内容才生效,等等注解。即自动配置类是需要在一定条件下才能生效

可以通过在 `application.properties` 文件中添加 `debug=true` 来打印出自动配置报告,看到哪一类自动配置类启动了哪一些没启动

总结:
1. xxxAutoConfiguration:自动配置类,给容器添加组件
2. xxxProperties:封装配置文件中相关属性