cav*_*llo 4 java spring amazon-s3 spring-cloud spring-cloud-aws
我最近将 SpringCloud 项目从 Brixton 升级到 Finchley,一切都工作正常。我正在开发 Finchley.SR2,没有遇到任何问题,但每当我将项目升级到 Finchley.RELEASE(这是我所做的唯一更改)时,项目就无法启动。
原因是项目找不到Bean AmazonS3Client:
...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: {}
Run Code Online (Sandbox Code Playgroud)
这些是我之前的相关配置和类:
构建.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'
...
}
...
Run Code Online (Sandbox Code Playgroud)
S3Config.java(创建 AmazonS3/AmazonS3Client Bean 的类)
...
@Configuration
public class S3Config {
@Bean
public AmazonS3 amazonS3() {
return AmazonS3ClientBuilder.standard()
.withCredentials(new DefaultAWSCredentialsProviderChain())
.build();
}
}
Run Code Online (Sandbox Code Playgroud)
StorageService(找不到Bean的类)
...
@Service
public class StorageService {
private final AmazonS3Client amazonS3Client;
@Autowired
public StorageService(AmazonS3Client amazonS3Client) {
this.amazonS3Client = amazonS3Client;
}
...
}
Run Code Online (Sandbox Code Playgroud)
这是升级到 Finchley.Release 时对build.gradle文件所做的唯一更改:
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:Finchley.RELEASE"
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试寻找任何丢失的库并调整我能找到的所有配置,但似乎没有任何效果。
在与 Spring 维护者进行简短交谈后,找到了解决方案。
看来我的错误在于假设 BeanAmazonS3应该总是作为AmazonS3ClientBean 被发现,只是因为其中一个实现了另一个。它能在以前的 Spring 版本上运行纯属幸运。
创建 的正确方法AmazonS3Client如下:
@Configuration
public class S3Config {
@Bean
public static AmazonS3Client amazonS3Client() {
return (AmazonS3Client) AmazonS3ClientBuilder.standard()
.withCredentials(new DefaultAWSCredentialsProviderChain())
.build();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8817 次 |
| 最近记录: |