首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >创建名为“amazonS3Client”的bean时出错:当前正在创建请求bean :是否存在不可解析的循环引用?

创建名为“amazonS3Client”的bean时出错:当前正在创建请求bean :是否存在不可解析的循环引用?
EN

Stack Overflow用户
提问于 2019-11-29 00:51:05
回答 1查看 4K关注 0票数 0

一切都启动了,因为Spring没有正确配置SimpleStorageProtocolResolver。这个类负责在使用ResourceLoader.时处理s3://协议有关更多详细信息,请参见问题:无法在Spring示例中转换为org.springframework.core.io.WritableResource

所以,我不得不手动创建它。但是我也在使用LocalStack解决方案(https://github.com/localstack/localstack),由于Spring没有手动配置端点的选项,所以我有(猜怎么着?)手动创建AmazonS3Client

问题来了,当我在类S3Configuration类中创建两个bean时(参见下面),只是跳过bean的创建。而且,当我试图将其连接到S3Handler类(请参见下面)时,会发生这样的错误:创建名为S3Handler的bean时出错:是否存在不可解析的循环引用?

但是,这里又来了一件事情,在这些类之间没有循环引用。

我已经简化了这个项目,所以我可以在这里发布:

pom.xml

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.7.RELEASE</version>
        <relativePath/>
    </parent>
    <groupId>io.mobi7.ms</groupId>
    <artifactId>mobi7-temp-ms-ibutton-worker</artifactId>
    <version>1.0.0-SNAPSHOT</version>

    <properties>
        <spring-cloud-version>2.1.4.RELEASE</spring-cloud-version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-aws</artifactId>
            <version>${spring-cloud-version}</version>
        </dependency>
    </dependencies>

</project>

S3Configuration.java

代码语言:javascript
复制
package io.mobi7.ms.ibutton.worker;

import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.aws.core.io.s3.SimpleStorageProtocolResolver;
import org.springframework.cloud.aws.core.region.RegionProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.DefaultResourceLoader;

@Configuration
public class S3Configuration {

    @Bean("amazonS3Client") // THIS METHOD IS NOT BEING CALLED!!!
    public AmazonS3 amazonS3Client(AWSCredentialsProvider credentialsProvider,
                                   RegionProvider regionProvider,
                                   @Value("${cloud.aws.s3.default-endpoint}") String endpoint) {
        return AmazonS3ClientBuilder.standard()
            .withCredentials(credentialsProvider)
            .withEndpointConfiguration(
                new AwsClientBuilder.EndpointConfiguration(endpoint, regionProvider.getRegion().getName()))
            .build();
    }

    @Autowired  // THIS METHOD IS NOT BEING CALLED!!!
    public void configureResourceLoader(@Qualifier("amazonS3Client") AmazonS3 amazonS3Client,
                                        DefaultResourceLoader resourceLoader) {
        SimpleStorageProtocolResolver simpleStorageProtocolResolver = new SimpleStorageProtocolResolver(amazonS3Client);
        // As we are calling it by hand, we must initialize it properly.
        simpleStorageProtocolResolver.afterPropertiesSet();
        resourceLoader.addProtocolResolver(simpleStorageProtocolResolver);
    }
}

S3Handle.java

代码语言:javascript
复制
package io.mobi7.ms.ibutton.worker;

import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.S3ObjectSummary;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

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

@Component
public class S3Handler {

    private AmazonS3 amazonS3Client;

    @Autowired
    public S3Handler(@Qualifier("amazonS3Client") AmazonS3 amazonS3Client) {
        this.amazonS3Client = amazonS3Client;
    }

    public List<String> listFiles(String bucketName) {
        return amazonS3Client.listObjects(bucketName).getObjectSummaries().stream().map(S3ObjectSummary::getKey)
            .collect(Collectors.toList());
    }
}

Application.java

代码语言:javascript
复制
package io.mobi7.ms.ibutton.worker;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        new SpringApplicationBuilder(Application.class).web(WebApplicationType.NONE).run(args);
    }

    @Autowired
    public void listFiles(S3Handler s3Handler) {
        s3Handler.listFiles("default-s3-bucket").forEach(System.out::println);
    }
}

application.properties

代码语言:javascript
复制
cloud.aws.region.static=us-east-1
cloud.aws.stack.auto=false
cloud.aws.s3.default-endpoint=http://localhost:4572

有什么想法吗,为什么会发生这种事?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-11-29 16:41:57

@Autowired注释标记了构造对象状态的方法(例如setter或方法注入)。参见“自动处理方法”一节这里

这两种方法都不用于此需要,而您使用的都是自动加载器。

这个问题是关于春天的。所有其他标签都可以移除。

更新:类似的情况涵盖这里

不管建议是什么,我不会保留一个上帝配置类,而是将您的配置类划分为两个类--用于S3客户端的工厂和资源加载器的配置。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59097582

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档