org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为“s3Controller”的bean时出错:表达了不满足的依赖关系

问题描述 投票:0回答:1
我正在使用 Maven 在 Java 17 上开发 AWS SDK v2 spring boot 项目,我尝试运行该文件,但它给我一个错误,提示“org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为 's3Controller 的 bean 时出错” ':通过字段's3Service'表达的依赖关系不满足:创建文件中定义的名称为's3Service'的bean时出错[/Users/aswinshaji/Documents/workspace-spring-tool-suite-4-4.22.0.RELEASE/webflux-demo/ target/classes/com/example/demo/services/S3Service.class]:通过构造函数参数 0 表达的依赖关系不满足:创建名称为“s3Config”的 bean 时出错:自动装配依赖关系注入失败”。 “创建文件中定义的名为“s3Service”的 bean 时出错”。 “创建名称为“s3Config”的 bean 时出错:自动装配依赖项注入失败”。我尽我所能尝试了一切,但无法解决问题。谁能帮我解决这个问题?谢谢

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); } }
S3控制器

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(); } }
S3配置

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()); } }
S3服务

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
应用程序.属性

java spring-boot maven aws-sdk
1个回答
0
投票
我的代码和你的一样,错误也一样。修改pom文件中s3的版本后,错误消失,运行正常。

<dependency> <groupId>software.amazon.awssdk</groupId> <artifactId>s3</artifactId> <version>2.25.52</version> </dependency>
    
© www.soinside.com 2019 - 2024. All rights reserved.