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

Spring Boot AWS SDK v2项目UnsatisfiedDependencyException问题求助

问题排查:Spring Boot AWS SDK v2 依赖注入失败

我基于Java 17、Maven开发Spring Boot AWS SDK v2项目,启动时抛出org.springframework.beans.factory.UnsatisfiedDependencyException错误:创建s3Controller Bean失败,其依赖的s3Service Bean创建失败,而s3Service依赖的s3Config Bean自动注入依赖失败。

相关代码

S3Controller

package com.example.demo.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.services.S3Service;

@RestController
@RequestMapping("/file")
public class S3Controller {
    
    @Autowired
    private S3Service s3Service;
    @GetMapping("/listAllObjects")
    public ResponseEntity<List<String>> viewObjects() {
        return new ResponseEntity<>(s3Service.listObjects(),HttpStatus.OK);
    }
}

S3Config

package com.example.demo.entities;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;

@Configuration
public class S3Config {
    @Value("${region}")
    private String region;

    @Value("${accesskey}")
    private String accessKey;

    @Value("${secretKey}")
    private String secretKey;

    @Bean
    public S3Client s3Client() {
        AwsBasicCredentials credentials = AwsBasicCredentials.create(accessKey, secretKey);
        return S3Client.builder()
                .credentialsProvider(StaticCredentialsProvider.create(credentials))
                .region(Region.of(region))
                .build();
    }
}

S3Service

package com.example.demo.services;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.ListObjectsResponse;
import software.amazon.awssdk.services.s3.model.S3Object;

@Service
public class S3Service {
    private final S3Client s3Client;
    private final String bucketName;

    public S3Service(S3Client s3Client, @Value("${bucketName}") String bucketName) {
        this.s3Client = s3Client;
        this.bucketName = bucketName;
    }

    public List<String> listObjects() {
        ListObjectsResponse response = s3Client.listObjects(builder -> builder.bucket(bucketName));
        return response.contents().stream().map(S3Object::key).collect(Collectors.toList());
    }
}

application.properties

spring.application.name=webflux-demo
aws.region=${region}
aws.accessKey=${accessKey}
aws.secretKey=${secretKey}
aws.bucketName=${bucketName}
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

问题原因及修复方案

1. 配置键不匹配(核心问题)

application.properties中所有AWS配置都带有aws.前缀,但代码中@Value注解引用的键未添加该前缀,导致Spring无法读取配置值,进而无法创建Bean。

修复:

  • 修改S3Config中的@Value注解:
@Value("${aws.region}")
private String region;

@Value("${aws.accessKey}")
private String accessKey;

@Value("${aws.secretKey}")
private String secretKey;
  • 修改S3Service中的@Value注解:
public S3Service(S3Client s3Client, @Value("${aws.bucketName}") String bucketName) {
    this.s3Client = s3Client;
    this.bucketName = bucketName;
}

2. 配置占位符无实际值

application.properties中使用的${region}等占位符没有实际值来源,需替换为真实AWS配置或通过环境变量传递:

aws.region=us-east-1
aws.accessKey=你的AWS访问密钥
aws.secretKey=你的AWS秘密密钥
aws.bucketName=你的S3桶名称

3. 可选优化:使用构造器注入

Spring推荐使用构造器注入替代@Autowired,提升代码可测试性:

private final S3Service s3Service;

public S3Controller(S3Service s3Service) {
    this.s3Service = s3Service;
}

4. 确认配置类扫描范围

确保主启动类所在包为com.example.demo,这样com.example.demo.entities下的S3Config会被自动扫描。若主启动类在更深层级,需添加@ComponentScan指定扫描范围。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 18:03:16