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

安装AWS CLI后Spring Boot应用创建AmazonS3 Bean失败

解决Spring Boot集成AWS S3时的java.lang.NoSuchFieldError: INSTANCE异常

问题场景

Spring Boot + Java项目原本通过application.properties配置凭证创建AmazonS3 Bean可正常运行,安装本地AWS CLI后,创建Bean时持续抛出java.lang.NoSuchFieldError: INSTANCE异常,尝试过配置AWS CLI、卸载CLI、更换凭证均无效。

配置类代码:

@Configuration
public class StorageConfiguration {

    @Value("${cloud.aws.credentials.access-key}")
    private String accessKey;
    @Value("${cloud.aws.credentials.secret-key}")
    private String accessSecret;
    @Value("${cloud.aws.region.static}")
    private String region;

    @Bean
    public AmazonS3 s3Client() {
        AWSCredentials credentials = new BasicAWSCredentials(accessKey, accessSecret);
        return AmazonS3ClientBuilder.standard()
                .withCredentials(new AWSStaticCredentialsProvider(credentials))
                .withRegion(region).build();
    }

}

核心异常栈:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 's3Client' defined in class path resource [com/cinemalab/aws/service/fileService/configurations/StorageConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.amazonaws.services.s3.AmazonS3]: Factory method 's3Client' threw exception; nested exception is java.lang.NoSuchFieldError: INSTANCE
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:658) ~[spring-beans-5.3.20.jar:5.3.20]

项目依赖:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-java-sdk-s3</artifactId>
        <version>1.12.504</version>
    </dependency>

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.0</version>
    </dependency>

</dependencies>

核心原因

这个异常100%是依赖版本冲突导致:AWS SDK内部依赖的核心组件(如aws-java-sdk-core、Apache HttpClient、Jackson)出现版本不匹配,或者项目中其他依赖间接引入了不同版本的AWS相关包,导致类加载时找不到预期的静态字段INSTANCE。AWS CLI安装只是触发点,本质是依赖树的冲突被激活。

解决步骤

1. 排查依赖冲突

用依赖分析工具找出冲突包:

  • Maven执行:mvn dependency:tree
  • Gradle执行:./gradlew dependencies

重点关注com.amazonaws:aws-java-sdk-core、org.apache.httpcomponents:httpclient这类核心依赖,看是否存在多个版本共存的情况。

2. 用AWS BOM锁定统一版本

在pom.xml中添加dependencyManagement,强制所有AWS相关依赖使用同一版本:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk-bom</artifactId>
            <version>1.12.504</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

然后修改s3依赖,移除手动指定的版本号,让它自动继承BOM的版本:

<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-java-sdk-s3</artifactId>
</dependency>

3. 排除冲突的传递依赖

如果发现其他依赖(比如spring-boot-starter-web)间接引入的组件版本和AWS SDK不兼容,在对应依赖中排除冲突包。比如HttpClient版本冲突的话:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
        </exclusion>
    </exclusions>
</dependency>

4. 清理本地仓库并重建

执行命令清理本地缓存的旧依赖,避免版本残留干扰:

mvn clean install -U

5. 显式禁用自动凭证/区域发现(可选)

AWS SDK会自动尝试从环境变量、CLI配置等渠道加载凭证,可能引发加载逻辑冲突,显式关闭自动加载:
修改Bean创建代码:

@Bean
public AmazonS3 s3Client() {
    AWSCredentials credentials = new BasicAWSCredentials(accessKey, accessSecret);
    return AmazonS3ClientBuilder.standard()
            .withCredentials(new AWSStaticCredentialsProvider(credentials))
            .withRegion(region)
            .disableAutomaticRegionDiscovery() // 禁用自动区域发现
            .build();
}

验证

完成上述步骤后重启项目,检查AmazonS3 Bean是否正常初始化,同时再次执行依赖树命令确认无版本冲突。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 12:35:06