SpringBoot 2.1.4升级至2.5.0后AWS ECS部署读取Secrets Manager异常
问题根因
Spring Boot 2.4 版本开始默认弃用了原有的bootstrap上下文加载逻辑,而AWS Secrets Manager的配置属性注入依赖Spring Cloud的bootstrap上下文初始化流程,也就是你观察到的PropertySourceBootstrapConfiguration Bean没有被加载的核心原因。
你之前添加的spring.config.use-legacy-processing配置没有生效,是因为你把该配置放在了aws profile的激活条件块内,而bootstrap上下文的加载时机早于profile激活阶段,此时aws profile还未生效,配置无法被读取。
解决方案
方案一(推荐,适配新版Spring Cloud规范)
无需开启兼容模式,直接引入bootstrap上下文支持依赖即可:
- 在
build.gradle的依赖中添加:
implementation 'org.springframework.cloud:spring-cloud-starter-bootstrap'
- 修复依赖版本不兼容问题:
你当前使用的Spring Boot 2.5.0对应Spring Cloud 2020.0.x版本系列,而你引入的spring-cloud-starter-aws-secrets-manager-config:2.2.6.RELEASE属于Hoxton版本系列,和Spring Boot 2.5.x不兼容,会导致自动配置失效。建议通过BOM统一管理版本:
- 打开你注释的dependencyManagement配置,修改为:
dependencyManagement { imports { mavenBom 'org.springframework.cloud:spring-cloud-dependencies:2020.0.5' mavenBom 'io.awspring.cloud:spring-cloud-aws-dependencies:2.3.5' } }
- 调整原有AWS Secrets Manager相关依赖声明,移除硬编码的版本号:
implementation 'org.springframework.cloud:spring-cloud-context' implementation 'io.awspring.cloud:spring-cloud-starter-aws-secrets-manager-config' compile group: 'com.amazonaws', name: 'aws-java-sdk-secretsmanager'
方案二(兼容旧版逻辑)
如果你需要沿用旧版的配置加载逻辑:
- 新增
bootstrap.yml文件放在resources根目录,添加以下配置:
spring: cloud: aws: secretsmanager: enabled: true config: use-legacy-processing: true
- 删除application.yml中放在aws profile块内的
use-legacy-processing配置。 - 同样按方案一的要求修复依赖版本兼容问题。
内容的提问来源于stack exchange,提问作者sromit
相关产品推荐
相关产品推荐

