在 Spring Boot 应用开发中,合理管理和加载配置是非常重要的。以往,我们通常使用 bootstrap.yaml/properties 方式来提前导入外部配置。但在 Spring Boot 2.4 及以上版本中,引入了 spring.config.import 机制,提供了更灵活的配置加载方式。

1. 传统的 bootstrap.yaml/properties 方式(Spring Boot 2.3 及以下)

在 Spring Boot 2.3 及以下版本,bootstrap.yaml(或 bootstrap.properties)主要用于加载外部配置,例如 Nacos。这种方式通常用于 Spring Cloud 组件初始化阶段。

示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# bootstrap.yaml
spring:
cloud:
nacos:
config:
name: test.yml
group: DEFAULT_GROUP
server-addr: 127.0.0.1:8848
extension-configs:
- dataId: test01.yml
group: group_01
- dataId: test02.yml
group: group_02
refresh: false

缺点:

  • bootstrap.yamlapplication.yaml 配置优先级不同,容易混淆。
  • 在 Spring Boot 2.4 及以上版本中已被 spring.config.import 取代。

2. spring.config.import 方式(Spring Boot 2.4+)

从 Spring Boot 2.4 开始,官方推荐使用 spring.config.import 来导入额外的配置源。

示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# application.yaml
spring:
application:
name: auth-service
profiles:
active: dev
cloud:
nacos:
server-addr: ${flying-forum.nacos.server-addr}
username: ${flying-forum.nacos.username}
password: ${flying-forum.nacos.password}
config:
import:
- optional:classpath:application-${spring.profiles.active}.yaml # 导入所在环境对应配置
- optional:nacos:shared-sentinel.yaml # 示例:导入Sentinel,如果配置不存在不会报错
- optional:nacos:test01.yml?group=group_01 # 覆盖默认 group,监听 group_01:test01.yml
- optional:nacos:test02.yml?group=group_02&refreshEnabled=false # 不开启动态刷新
- nacos:test03.yml # 在拉取nacos配置异常时会快速失败,会导致 spring 容器启动失败

注意事项:

  1. 如果使用 spring.config.import,就不能使用 bootstrap.yaml/properties 方式了;
  2. 如果引入 spring-cloud-starter-alibaba-nacos-config,但未使用 spring.config.import 方式导入 Nacos 配置,Spring Boot 启动时会报错;
  3. 可以通过 spring.cloud.nacos.config.import-check.enabled=false 关闭 Nacos 配置检查,但不推荐;
  4. 想保留 bootstrap.yaml 方式,可以添加 spring-cloud-starter-bootstrap 依赖。
1
2
3
4
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>

3. 其他配置加载方式

通过 EnvironmentPostProcessor 代码方式动态加载配置

1
2
3
4
5
6
7
8
public class CustomConfigLoader implements EnvironmentPostProcessor {
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
MapPropertySource customSource = new MapPropertySource("customConfig",
Collections.singletonMap("custom.property", "customValue"));
environment.getPropertySources().addLast(customSource);
}
}

然后在 META-INF/spring.factoriesMETA-INF/spring/org.springframework.boot.env.EnvironmentPostProcessor 中注册:

1
org.springframework.boot.env.EnvironmentPostProcessor=com.example.config.CustomConfigLoader

通过 @PropertySource 读取自定义配置

1
2
3
4
5
6
@Configuration
@PropertySource("classpath:custom.properties")
public class CustomConfig {
@Value("${custom.property}")
private String customProperty;
}

通过命令行参数导入配置

1
java -jar my-app.jar --spring.config.location=file:./config/custom-config.yaml

4. 总结

方式 适用场景 适用版本
bootstrap.yaml 旧版本 Spring Cloud 配置加载 2.3 及以下
spring.config.import 远程/本地额外配置导入 2.4+
EnvironmentPostProcessor 动态加载配置 任意版本
@PropertySource 读取额外配置文件 任意版本
命令行参数 临时指定配置路径 任意版本

从 Spring Boot 2.4 开始,spring.config.import 方式是官方推荐的配置加载方式,它比 bootstrap.yaml 更加灵活,也更容易理解。在不同的应用场景下,我们可以选择合适的方式来管理 Spring Boot 配置。