一、基础优先级顺序
1. 文件位置优先级(从高到低)
bash
1. 当前目录的 /config 子目录
└── ./config/
2. 当前目录
└── ./
3. classpath 下的 /config 包
└── classpath:/config/
4. classpath 根路径
└── classpath:/
2. 文件类型优先级
在同一目录下,如果有多个配置文件,优先级顺序为:
application.properties>application.yml
示例 :如果 application.properties 和 application.yml 在同一个目录,application.properties 中的配置会覆盖 application.yml 中相同的配置。
二、多环境配置优先级
1. 多环境文件命名规则
bash
application-{profile}.properties
application-{profile}.yml
2. 完整优先级顺序(从高到低)
bash
1. 命令行参数
└── java -jar app.jar --server.port=8081
2. 环境变量(操作系统级)
└── export SERVER_PORT=8081
3. 特定 Profile 的配置文件(按位置优先级)
└── application-dev.properties(或 yml)
4. 默认配置文件
└── application.properties(或 yml)
5. Spring Boot 内置默认配置
3. 多环境配置示例
假设有 dev、prod、test 三个环境:
bash
# application.properties(默认配置)
server.port=8080
app.name=MyApp
# application-dev.properties
server.port=8081
app.debug=true
# application-prod.properties
server.port=80
app.debug=false
读取规则:
-
使用
spring.profiles.active=dev时-
先读取
application.properties -
再读取
application-dev.properties,覆盖相同属性 -
最终
server.port=8081
-
三、重复覆盖顺序详解
1. 不同位置配置文件的覆盖
假设存在以下位置的配置文件:
bash
# 位置1: classpath:/application.properties
server.port=8080
# 位置2: classpath:/config/application.properties
server.port=8081
# 位置3: ./application.properties
server.port=8082
# 位置4: ./config/application.properties
server.port=8083
最终生效顺序 :位置4 > 位置3 > 位置2 > 位置1
最终端口:8083
2. 不同格式配置文件的覆盖
bash
yaml
# application.yml
server:
port: 8080
servlet:
context-path: /api
properties
# application.properties
server.port=8081
结果:
-
server.port=8081(properties 覆盖 yml) -
server.servlet.context-path=/api(保持不变)
3. 多环境 + 多位置的复杂覆盖
bash
# classpath:/application.properties
server.port=8080
# classpath:/application-dev.properties
server.port=8081
# classpath:/config/application.properties
server.port=8082
# classpath:/config/application-dev.properties
server.port=8083
使用 --spring.profiles.active=dev 时:
-
加载顺序:
-
classpath:/application.properties (8080)
-
classpath:/config/application.properties (8082) - 覆盖
-
classpath:/application-dev.properties (8081) - 覆盖
-
classpath:/config/application-dev.properties (8083) - 覆盖
-
-
最终端口:8083
四、配置加载顺序完整清单
Spring Boot 配置加载顺序(从高到低):
bash
1. 命令行参数
2. Java 系统属性 (System.getProperties())
3. 操作系统环境变量
4. JNDI 属性 (java:comp/env)
5. RandomValuePropertySource (random.* 属性)
6. 特定 Profile 的配置文件(jar 包外)
7. 特定 Profile 的配置文件(jar 包内)
8. 默认配置文件(jar 包外)
9. 默认配置文件(jar 包内)
10. @PropertySource 注解配置
11. SpringApplication 默认属性
五、实际应用建议
1. 配置文件组织示例
项目结构:
├── src/main/resources/
│ ├── application.yml # 通用配置
│ ├── application-dev.yml # 开发环境
│ ├── application-test.yml # 测试环境
│ └── application-prod.yml # 生产环境
└── config/ # 外部配置目录(生产环境)
└── application-prod.yml # 覆盖内部配置
2. 优先级最佳实践
bash
yaml
# 1. 使用 application.yml 存放默认配置
spring:
profiles:
active: dev
application:
name: my-service
# 2. 环境特定配置放在 application-{profile}.yml
# application-dev.yml
server:
port: 8080
servlet:
context-path: /dev
# 3. 敏感信息通过环境变量或命令行覆盖
# java -jar app.jar --db.password=${DB_PASSWORD}
3. 验证配置优先级的代码示例
java
@Component
public class ConfigPrinter implements ApplicationRunner {
@Value("${server.port}")
private String port;
@Value("${app.name:default}")
private String appName;
@Autowired
private Environment environment;
@Override
public void run(ApplicationArguments args) {
System.out.println("Server Port: " + port);
System.out.println("App Name: " + appName);
// 查看所有激活的配置文件
Arrays.stream(environment.getActiveProfiles())
.forEach(profile -> System.out.println("Active Profile: " + profile));
}
}
六、特殊情况处理
1. 使用 spring.config.location 强制指定
bash
java -jar app.jar --spring.config.location=/opt/config/application.yml
此时只加载指定位置的配置文件,其他位置不加载。
2. 使用 spring.config.additional-location 补充
bash
java -jar app.jar --spring.config.additional-location=/opt/config/
额外添加配置位置,仍然会加载默认位置的配置。
3. 配置文件的合并规则
-
简单类型:后面的覆盖前面的
-
集合/Map类型:完全覆盖,不是合并
-
对象类型:属性级别的覆盖
示例:
bash
yaml
# application.yml
app:
config:
key1: value1
key2: value2
# application-dev.yml
app:
config:
key1: new-value1
结果 :只有 key1 被覆盖,key2 仍然保留。
理解这些优先级规则对于管理复杂的 Spring Boot 应用配置非常重要,可以避免配置冲突和意外的行为。