1

我最近将我的 SpringCloud 项目从 Brixton 升级到了 Finchley,一切正常。我正在研究 Finchley.SR2 并且没有任何问题,但是每当我将项目升级到 Finchley.RELEASE (这是我所做的唯一更改)时,项目都无法启动。

原因是项目找不到AmazonS3ClientBean:

...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.

4

1 回答 1

6

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();
    }
}
于 2018-07-03T23:23:50.440 回答