升级Spring Boot后Spring Cloud Bootstrap配置源被添bootstrapProperties-前缀问题
Spring Boot 2.5.8 + Spring Cloud 2020.0.x配置源带
bootstrapProperties-前缀的原因及初始化器失效分析 1. bootstrapProperties-前缀的由来
- Spring Cloud 2020.0.x(代号Ilford)开始,默认禁用了bootstrap上下文,这是Spring Cloud为对齐Spring Boot自动配置流程做出的核心调整。
- 当你引入
spring-cloud-starter-bootstrap依赖时,会重新启用bootstrap上下文。此时,bootstrap阶段加载的配置源(比如从配置服务器获取的configService配置)会被Spring Cloud包装成带有bootstrapProperties-前缀的PropertySource实例。 - 这个前缀的作用是标识配置来自bootstrap初始化阶段,目的是隔离bootstrap上下文与应用上下文的配置,避免两者的配置源出现命名冲突,同时明确配置的加载优先级与来源。
2. 自定义初始化器CustomEnvironmentDecryptApplicationInitializer未加载的原因
- 在旧版本Spring Cloud(如Edgware,对应Spring Boot 1.5.x)中,自定义
ApplicationContextInitializer可通过META-INF/spring.factories的org.springframework.context.ApplicationContextInitializer条目直接注册,会在bootstrap阶段自动执行。 - 新版本中,bootstrap上下文有独立的初始化流程:
- 要让初始化器在bootstrap阶段生效,必须将其注册到
org.springframework.cloud.bootstrap.BootstrapConfiguration(通过META-INF/spring.factories),或使用@BootstrapConfiguration注解标记配置类并定义初始化器Bean。 - 原注册方式只会让初始化器在应用上下文阶段执行,而此时bootstrap阶段的配置源(带
bootstrapProperties-前缀)尚未合并到应用上下文,导致解密逻辑无法处理目标配置。
- 要让初始化器在bootstrap阶段生效,必须将其注册到
3. 修复建议
- 调整自定义初始化器的注册方式:
- 在项目的
META-INF/spring.factories中添加:
或创建配置类并使用org.springframework.cloud.bootstrap.BootstrapConfiguration=com.esrx.inf.config.client.CustomEnvironmentDecryptApplicationInitializer@BootstrapConfiguration注解:@BootstrapConfiguration public class BootstrapDecryptConfig { @Bean public CustomEnvironmentDecryptApplicationInitializer customDecryptInitializer() { return new CustomEnvironmentDecryptApplicationInitializer(); } } - 确保初始化器逻辑能正确识别并处理带
bootstrapProperties-前缀的PropertySource,提取其中的配置内容进行解密。
- 在项目的
内容的提问来源于stack exchange,提问作者mijailciencias
相关产品推荐
相关产品推荐

