You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

升级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-前缀)尚未合并到应用上下文,导致解密逻辑无法处理目标配置。

3. 修复建议

  • 调整自定义初始化器的注册方式:
    1. 在项目的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();
          }
      }
      
    2. 确保初始化器逻辑能正确识别并处理带bootstrapProperties-前缀的PropertySource,提取其中的配置内容进行解密。

内容的提问来源于stack exchange,提问作者mijailciencias

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 01:10:27