Spring Cloud AWS - AmazonS3客户端始终为null

时间:2015-07-13 10:33:52

标签: spring amazon-web-services amazon-s3 spring-cloud

我正尝试使用Spring Cloud项目将文件上传到Amazon S3 Bucket。

根据文件:

  

" com.amazonaws.services.s3.transfer.TransferManager可以轻松实现   在应用程序代码中创建并注入预配置   已经创建的com.amazonaws.services.s3.AmazonS3客户端   Spring Cloud AWS资源加载器配置。"

此示例代码不起作用:

public class SimpleResourceLoadingBean {

@Autowired
private AmazonS3 amazonS3;

public void withTransferManager() {
    TransferManager transferManager = new TransferManager(this.amazonS3);
    transferManager.upload("myBucket","filename",new File("someFile"));
}

}

amazonS3变量始终为空。

我的春天上下文配置如下:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:aws-context="http://www.springframework.org/schema/cloud/aws/context" 
xmlns:mvc="http://www.springframework.org/schema/mvc" 
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd 
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd 
http://www.springframework.org/schema/cloud/aws/context
http://www.springframework.org/schema/cloud/spring-cloud-aws-context.xsd">

<aws-context:context-credentials>
 <aws-context:simple-credentials access-key="KEY" secret-key="SECRET" />
</aws-context:context-credentials>

<aws-context:context-region region="eu-central-1"/>

<aws-context:context-resource-loader/>

<bean id="awsTransferManager" class="mytest.cloud.aws.AwsTransferManager" />

我正在使用这个类进行测试:

public class AwsTransferManager {

@Autowired
private AmazonS3 amazonS3;

public void upload() throws AmazonServiceException, AmazonClientException, InterruptedException {
    TransferManager tx = new TransferManager(amazonS3);
    System.out.println(amazonS3); //always null
}

测试:

@Test
public void awsTest() throws Exception {
    ApplicationContext cxt = new ClassPathXmlApplicationContext("META-INF/spring/spring-context.xml");
    AwsTransferManager manager = (AwsTransferManager)getContext().getBean("awsTransferManager");
    manager.upload();
}

1 个答案:

答案 0 :(得分:0)

好的,我想通了,我错过了

<context:annotation-config />

参与我的春季配置。