org.springframework.beans.factory.BeanCreationException:创建名称为“s3Config”的bean时出错:自动装配依赖项注入失败

问题描述 投票:0回答:1

我正在使用 Maven 在 Java 17 上开发 AWS SDK v2 spring boot 项目,我尝试运行该文件,但收到一条错误消息“org.springframework.beans.factory.BeanCreationException:创建名称为“s3Config”的 bean 时出错:自动装配依赖项的注入失败”。我尽了一切努力,但到目前为止还无法解决问题。谁能帮我解决这个问题吗?谢谢

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

演示应用程序


package com.example.demo.config;

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
    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.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 com.example.demo.services.S3Service;

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.services;


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;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class S3Service {
    @Value("${bucketName}")
    private String bucketName;

    private final S3Client s3Client;

    public S3Service(S3Client s3Client) {
        this.s3Client = s3Client;
    }

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

S3服务

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

应用程序.属性

java amazon-web-services spring-boot maven java-17
1个回答
0
投票

AWS SDK for Java V2 服务客户端在 Spring Boot 应用程序中正常工作。但是,我不喜欢尝试自动装配 AWS 服务类。

尝试采取不同的方法。

控制器...

public class S3Controller {
    @Autowired
    private Foo foo;

    public void someMethod() {
        // Use the S3Service through the Foo class
        foo.getS3Service().someOperation();
}



@Component
public class Foo {
    @Singleton
    @Bean
    public S3Service getS3Service() {
    return S3Client.builder()
         .region(Region.US_WEST_2)
         .build();
    }

    // Get the byte[] from this Amazon S3 object.
    public byte[] getObjectBytes (String bucketName, String keyName) {
        s3 = getClient();
        try {
            GetObjectRequest objectRequest = GetObjectRequest
                    .builder()
                    .key(keyName)
                    .bucket(bucketName)
                    .build();

            ResponseBytes<GetObjectResponse> objectBytes = s3.getObjectAsBytes(objectRequest);
            return objectBytes.asByteArray();

        } catch (S3Exception e) {
            System.err.println(e.awsErrorDetails().errorMessage());
            System.exit(1);
        }
        return null;
    }

 }
    

通过在 S3Controller 中自动装配 Foo 类(使用 AWS Service CLient 的类),您可以保持清晰的关注点分离。 S3Controller 负责处理控制器级逻辑,而 Foo 类封装了服务级逻辑,包括S3Service的管理。

S3Service 声明为 Foo 类中的单例,可以让您轻松管理整个应用程序中 S3Service 实例的生命周期和共享。

您可以在此处的 AWS COde Lib 中找到使用 Amazon S3 的 Spring BOOT 应用程序示例:

创建使用适用于 Java 的 AWS 开发工具包分析照片的动态 Web 应用程序

© www.soinside.com 2019 - 2024. All rights reserved.