Spring Boot 3.3.0中排除AWS自动配置类报错的解决方法
问题根源
Spring Boot 3.3.0对@EnableAutoConfiguration的排除规则做了严格校验——必须排除标注了@AutoConfiguration的类,而你用的Spring Cloud AWS 2.2.5是适配Spring Boot 2.x的老版本,其中的ContextXXXAutoConfiguration类并没有标注@AutoConfiguration注解,所以直接用@EnableAutoConfiguration(exclude)会触发报错。同时这个老版本和Spring Boot 3.x的Jakarta EE规范不兼容,长远来看存在维护风险。
方案一:升级到兼容的Spring Cloud AWS版本(推荐)
Spring Cloud AWS已迁移至io.awspring.cloud组织,3.x版本完全适配Spring Boot 3.x,步骤如下:
替换Pom依赖
删除原来的org.springframework.cloud相关AWS依赖,替换为最新的awspring cloud依赖:<dependency> <groupId>io.awspring.cloud</groupId> <artifactId>spring-cloud-starter-aws-secrets-manager-config</artifactId> <version>3.2.0</version> <!-- 适配Spring Boot 3.2+,兼容3.3.0 --> </dependency>本地配置禁用AWS自动配置
创建application-local.yml(或properties),添加以下配置直接禁用AWS相关功能:spring: cloud: aws: enabled: false secretsmanager: enabled: false这样本地运行时(激活
localprofile)会完全跳过AWS相关配置,不会触发凭证加载逻辑。
方案二:不升级依赖,临时解决报错
如果暂时无法升级依赖,可通过以下两种方式规避问题:
方式1:通过配置文件禁用AWS自动配置
在application-local.properties中添加老版本支持的全局禁用配置:
spring.cloud.aws.enabled=false
该配置会让Spring Cloud AWS 2.2.5跳过所有自动配置类的加载,避免凭证报错。
方式2:提供本地模拟的凭证Bean
移除原来带@EnableAutoConfiguration(exclude)的配置类,新建一个本地配置类,提供一个空的凭证提供者Bean:
import com.amazonaws.auth.AWSCredentials; import com.amazonaws.auth.AWSCredentialsProvider; import com.amazonaws.auth.BasicAWSCredentials; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; @Configuration @Profile("local") class LocalAwsCredentialConfig { @Bean public AWSCredentialsProvider awsCredentialsProvider() { return new AWSCredentialsProvider() { @Override public AWSCredentials getCredentials() { // 返回模拟的空凭证,避免SDK加载真实凭证失败 return new BasicAWSCredentials("dummy-access-key", "dummy-secret-key"); } @Override public void refresh() { // 无需刷新 } }; } }
这个Bean会被AWS SDK优先使用,不会触发环境变量/系统属性的凭证加载流程,也就不会抛出SdkClientException。
内容的提问来源于stack exchange,提问作者MA1

