java - 将 SpringCloud Finchley 升级到 RELEASE 时缺少 AmazonS3Client Bean
问题描述
我最近将我的 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: {}
这些是我之前的相关配置和类:
构建.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"
}
}
I've tried looking for any missing library and tweaking all the configurations I can find, but none seem to take any effect.
解决方案
After a brief talk with the Spring maintainers, a solution was found.
It seems I was at fault by assuming that a Bean of AmazonS3
should always be found as an AmazonS3Client
Bean just because one implements the other. It was just pure luck that it worked on previous Spring versions.
The proper way to create an AmazonS3Client
would be the following:
@Configuration
public class S3Config {
@Bean
public static AmazonS3Client amazonS3Client() {
return (AmazonS3Client) AmazonS3ClientBuilder.standard()
.withCredentials(new DefaultAWSCredentialsProviderChain())
.build();
}
}
推荐阅读
- augmented-reality - 如何在定制的 Snapdragon Board 上运行 ARcore?
- reactjs - React-router-dom 链接更改 url 但不更新视图
- javascript - 如何验证用户的产品是否已经存在?
- docker - pod init 中 argo 图像的图像拉回退
- java - 添加两个矩阵的索引越界错误
- laravel - 每页样式表
- javascript - 如何修复导航器/窗口/文档在 Nuxt 中未定义
- tensorflow - 在使用 ImageDataGenerator 类进行图像数据增强时使用 TPU 训练卷积神经网络 (CNN) 时遇到问题?
- node.js - NodeJS 使用 JWT 令牌进行授权
- pattern-matching - 有没有办法使用 trivia/optima 进行模式匹配 FSet 或 Sycamore?