我最近将我的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 3/AmazonS3ClientBean的类)
...
@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;
}
...
}这是我在升级到build.gradle文件时对Finchley.Release文件所做的唯一更改:
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:Finchley.RELEASE"
}
}我已经尝试寻找任何缺失的库,并调整所有的配置,我可以找到,但似乎没有任何效果。
发布于 2018-07-03 23:23:50
在与Spring维护人员进行了简短的讨论之后,找到了一个解决办法。
似乎我错了,因为我假设AmazonS3的Bean应该总是作为AmazonS3Client Bean找到,因为其中一个实现了另一个。这只是纯粹的运气,它工作在以前的Spring版本。
创建AmazonS3Client的正确方法如下:
@Configuration
public class S3Config {
@Bean
public static AmazonS3Client amazonS3Client() {
return (AmazonS3Client) AmazonS3ClientBuilder.standard()
.withCredentials(new DefaultAWSCredentialsProviderChain())
.build();
}
}https://stackoverflow.com/questions/51034175
复制相似问题