首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在升级AmazonS3Client Finchley以发布SpringCloud Bean时缺少SpringCloud Bean

在升级AmazonS3Client Finchley以发布SpringCloud Bean时缺少SpringCloud Bean
EN

Stack Overflow用户
提问于 2018-06-26 02:43:40
回答 1查看 6.3K关注 0票数 1

我最近将我的SpringCloud项目从Brixton升级到Finchley,一切都很顺利。我在Finchley.SR2上工作,我没有任何问题,但是每当我将项目升级到Finchley.RELEASE (这是我所做的唯一更改),项目就无法启动。

原因是项目找不到AmazonS3Client Bean:

代码语言:javascript
复制
...Unsatisfied dependency expressed through constructor parameter 0; 
nested exception is 
  org.springframework.beans.factory.NoSuchBeanDefinitionException: 
    No qualifying bean of type 'com.amazonaws.services.s3.AmazonS3Client' available: 
      expected at least 1 bean which qualifies as autowire candidate. 
        Dependency annotations: {}

以下是我以前的相关配置和类:

build.gradle

代码语言:javascript
复制
buildscript {
    ext {
        springBootVersion = '2.0.2.RELEASE'
    }

    ...

    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        classpath('io.spring.gradle:dependency-management-plugin:1.0.5.RELEASE')
    }
}

apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
    
dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:Finchley.SR2"
    }
}

dependencies {
    ...
    compile('org.springframework.boot:spring-boot-starter-web')
    compile('org.springframework.cloud:spring-cloud-starter-aws')
    compile('org.springframework.cloud:spring-cloud-starter-config'
    ...
}

...

S3Config.java (创建AmazonS3 3/AmazonS3ClientBean的类)

代码语言:javascript
复制
...

@Configuration
public class S3Config {

    @Bean
    public AmazonS3 amazonS3() {
        return AmazonS3ClientBuilder.standard()
                .withCredentials(new DefaultAWSCredentialsProviderChain())
                .build();
    }
}

StorageService (找不到Bean的类)

代码语言:javascript
复制
...

@Service
public class StorageService {

    private final AmazonS3Client amazonS3Client;

    @Autowired
    public StorageService(AmazonS3Client amazonS3Client) {
        this.amazonS3Client = amazonS3Client;
    }

    ...
}

这是我在升级到build.gradle文件时对Finchley.Release文件所做的唯一更改:

代码语言:javascript
复制
dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:Finchley.RELEASE"
    }
}

我已经尝试寻找任何缺失的库,并调整所有的配置,我可以找到,但似乎没有任何效果。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-03 23:23:50

在与Spring维护人员进行了简短的讨论之后,找到了一个解决办法

似乎我错了,因为我假设AmazonS3的Bean应该总是作为AmazonS3Client Bean找到,因为其中一个实现了另一个。这只是纯粹的运气,它工作在以前的Spring版本。

创建AmazonS3Client的正确方法如下:

代码语言:javascript
复制
@Configuration
public class S3Config {

    @Bean
    public static AmazonS3Client amazonS3Client() {
        return (AmazonS3Client) AmazonS3ClientBuilder.standard()
                .withCredentials(new DefaultAWSCredentialsProviderChain())
                .build();
    }
}
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51034175

复制
相关文章

相似问题

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