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

时间:2018-06-26 02:43:41

标签: java spring amazon-s3 spring-cloud spring-cloud-aws

我最近将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时对 build.gradle 文件所做的唯一更改。发布:

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

我尝试寻找任何缺少的库并调整所有可以找到的配置,但是似乎没有任何作用。

1 个答案:

答案 0 :(得分:0)

在与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();
    }
}