文章目录
-
- [1. 概述](#1. 概述)
- [2. 配置文件加载机制](#2. 配置文件加载机制)
- [3. 配置文件优先级规则详解](#3. 配置文件优先级规则详解)
-
- [3.1 最高优先级配置文件](#3.1 最高优先级配置文件)
- [3.2 通过 include 指令加载的配置文件](#3.2 通过 include 指令加载的配置文件)
-
- [3.2.1 [application-security.yaml]- list中优先级最低](#3.2.1 [application-security.yaml]- list中优先级最低)
- [3.2.2 [application-xxl-job.yaml]- 优先级中等偏低](#3.2.2 [application-xxl-job.yaml]- 优先级中等偏低)
- [3.2.3 [application-flyway.yaml】 优先级中等](#3.2.3 [application-flyway.yaml】 优先级中等)
- [3.2.4 [application-default.yaml] - 优先级中等偏高](#3.2.4 [application-default.yaml] - 优先级中等偏高)
- [3.2.5 [application-file.yaml]- 优先级较高](#3.2.5 [application-file.yaml]- 优先级较高)
- [3.2.6 [application-postgresql.yaml] - 优先级最高(在 include 列表中)](#3.2.6 [application-postgresql.yaml] - 优先级最高(在 include 列表中))
- [3.3 基础配置文件](#3.3 基础配置文件)
- [4. 配置覆盖规则详解](#4. 配置覆盖规则详解)
-
- [4.1 属性覆盖原则](#4.1 属性覆盖原则)
- [4.2 Profile 特定配置优先级](#4.2 Profile 特定配置优先级)
- [4.3 配置属性的合并行为](#4.3 配置属性的合并行为)
- [5.2 具体配置覆盖示例](#5.2 具体配置覆盖示例)
1. 概述
在 Spring Boot 应用中,配置文件的加载顺序和优先级决定了最终生效的配置值。理解这一机制对于正确管理应用配置至关重要,特别是在复杂的多模块项目中,配置文件的组织和优先级关系直接影响应用的行为。
2. 配置文件加载机制
比如我们定义了如下的 profile 配置:
yaml
profiles:
active: ${PROFILES_ACTIVE:local}
include: security,xxl-job,flyway,default,file,${NIMBUS_DB_TYPE:postgresql}
这个配置定义了主配置文件和需要额外加载的配置文件集合。Spring Boot 会按照特定的顺序加载这些配置文件,并根据优先级规则决定最终的配置值。
3. 配置文件优先级规则详解
Spring Boot 遵循以下优先级顺序,数字越小优先级越高,后加载的配置会覆盖先加载的同名配置:
3.1 最高优先级配置文件
java
application-{active_profile}.yaml
- 优先级最高
- 这是当前激活的 profile 对应的配置文件
- 此文件中的配置会覆盖其他所有配置文件中的同名配置
3.2 通过 include 指令加载的配置文件
按 include 列表中的顺序依次加载,后加载的配置文件会覆盖先加载的同名配置:
3.2.1 [application-security.yaml]- list中优先级最低
- 首先加载安全相关的配置
- 包含认证、授权等安全相关配置项
- 在后续配置文件加载时可能被覆盖
3.2.2 [application-xxl-job.yaml]- 优先级中等偏低
- 加载分布式任务调度框架 XXL-JOB 的相关配置
- 可能覆盖安全配置中的某些属性
3.2.3 [application-flyway.yaml】 优先级中等
- 加载数据库迁移工具 Flyway 的配置
- 通常包含数据库迁移相关的属性
3.2.4 [application-default.yaml] - 优先级中等偏高
- 加载默认配置
- 通常包含通用的默认设置
3.2.5 [application-file.yaml]- 优先级较高
- 加载文件服务相关的配置
- 可能覆盖之前的通用配置
3.2.6 [application-postgresql.yaml] - 优先级最高(在 include 列表中)
- 最后加载数据库配置
- 由于是 include 列表中最后一个加载的文件,它的配置会覆盖 include 列表中其他文件的同名配置
- 但仍然低于 [application-{active_profile}.yaml]
3.3 基础配置文件
-
application.yaml\] 最终的基础配置
- 优先级最低,容易被其他配置文件覆盖
4. 配置覆盖规则详解
4.1 属性覆盖原则
- 在不同优先级的配置文件中,相同属性名的值会按照优先级规则进行覆盖
- 高优先级配置文件中的属性值会覆盖低优先级配置文件中的同名属性值
- 例如,如果 [application-security.yaml]中定义了
server.port=9001,而 [application-postgresql.yaml]中定义了server.port=9002,则最终生效的端口将是 9002
4.2 Profile 特定配置优先级
-
application-{profile}.properties\] 优先级最高高
4.3 配置属性的合并行为
- 对于复合类型的配置(如 lists, maps),Spring Boot 会根据属性来源进行合并或替换
- 简单属性(String, int, boolean 等)直接被高优先级配置覆盖
- 复杂属性可能涉及合并逻辑,需要注意配置设计
5.2 具体配置覆盖示例
假设有以下配置项分布在不同文件中:
[application-security.yaml]:
yaml
server:
port: 9001
logging:
level:
com.nimbus: DEBUG
[application-postgresql.yaml]
yaml
server:
port: 9002
spring:
datasource:
url: jdbc:postgresql://localhost:5432/mydb
[application-local.yaml]
yaml
server:
port: 9003
logging:
level:
com.nimbus: INFO
最终生效的配置:
yaml
server:
port: 9003 # 来自 application-local.yaml (最高优先级)
logging:
level:
com.nimbus: INFO # 来自 application-local.yaml (最高优先级)
spring:
datasource:
url: jdbc:postgresql://localhost:5432/mydb # 来自 application-postgresql.yaml