java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > SpringBoot配置文件优先级

一文带你深入了解SpringBoot配置文件的优先级

作者:Full Stack Developme

这篇文章主要为大家详细介绍了SpringBoot中配置文件的优先级以及实际应用建议,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下

一、基础优先级顺序

1. 文件位置优先级(从高到低)

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. 多环境文件命名规则

application-{profile}.properties
application-{profile}.yml

2. 完整优先级顺序(从高到低)

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 三个环境:

# 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 时

三、重复覆盖顺序详解

1. 不同位置配置文件的覆盖

假设存在以下位置的配置文件:

# 位置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. 不同格式配置文件的覆盖

# application.yml
server:
  port: 8080
  servlet:
    context-path: /api
properties
# application.properties
server.port=8081

结果

3. 多环境 + 多位置的复杂覆盖

# 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 时:

加载顺序:

最终端口:8083

四、配置加载顺序完整清单

Spring Boot 配置加载顺序(从高到低):

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. 优先级最佳实践

# 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. 验证配置优先级的代码示例

@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 强制指定

java -jar app.jar --spring.config.location=/opt/config/application.yml

此时只加载指定位置的配置文件,其他位置不加载。

2. 使用 spring.config.additional-location 补充

java -jar app.jar --spring.config.additional-location=/opt/config/

额外添加配置位置,仍然会加载默认位置的配置。

3. 配置文件的合并规则

示例

# application.yml
app:
  config:
    key1: value1
    key2: value2
# application-dev.yml
app:
  config:
    key1: new-value1

结果:只有 key1 被覆盖,key2 仍然保留。

理解这些优先级规则对于管理复杂的 Spring Boot 应用配置非常重要,可以避免配置冲突和意外的行为。

到此这篇关于一文带你深入了解SpringBoot配置文件的优先级的文章就介绍到这了,更多相关SpringBoot配置文件优先级内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文