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

Missing AmazonS3Client Bean when upgrading SpringCloud Finchley to RELEASE

我最近将我的 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: {}

这些是我之前的相关配置和classes:

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 的 class)

...

@Configuration
public class S3Config {

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

StorageService(找不到Bean的class)

...

@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"
    }
}

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

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