gpt4 book ai didi

java - 将 SpringCloud Finchley 升级到 RELEASE 时缺少 AmazonS3Client Bean

转载 作者:行者123 更新时间:2023-12-02 02:04:26 25 4
gpt4 key购买 nike

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

原因是项目找不到AmazonS3Client Bean:

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

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/AmazonS3Client Bean 的类)

...

@Configuration
public class S3Config {

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

StorageService(找不到Bean的类)

...

@Service
public class StorageService {

private final AmazonS3Client amazonS3Client;

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

...
}

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

dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:Finchley.RELEASE"
}
}

我尝试寻找任何丢失的库并调整我能找到的所有配置,但似乎没有任何效果。

最佳答案

与 Spring 维护者进行简短交谈后,a solution was found .

看来我的错误是假设 AmazonS3 的 Bean 应该始终作为 AmazonS3Client Bean 找到,只是因为其中一个实现了另一个。它能在以前的 Spring 版本上运行纯属运气。

创建 AmazonS3Client 的正确方法如下:

@Configuration
public class S3Config {

@Bean
public static AmazonS3Client amazonS3Client() {
return (AmazonS3Client) AmazonS3ClientBuilder.standard()
.withCredentials(new DefaultAWSCredentialsProviderChain())
.build();
}
}

关于java - 将 SpringCloud Finchley 升级到 RELEASE 时缺少 AmazonS3Client Bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51034175/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com