首页 > 解决方案 > 工厂方法在 Spring 中没有被正确调用

问题描述

我正在尝试创建一个 SNS 客户端 bean。

<bean id="snsClientBuilder" class="com.amazonaws.services.sns.AmazonSNSClientBuilder"
          factory-method="standard">
        <property name="credentials" ref="credntialsPair" />
        <property name="region" value="us-west-2" />
    </bean>

<bean id="snsClient" factory-bean="snsClientBuilder" factory-method="build"/>
/>

代码

    @Autowired
    private AmazonSNSClientBuilder snsClientBuilder;

    @Autowired
    private AmazonSNS snsClient;

    @Autowired
    private String notificationTopicArn; //Bean also correctly defined

     @Override
    public void sendNotification(final Notification notification) {
        final String message = notification.toJson();

         //wrapped in a try catch
         snsClient.publish(new PublishRequest().withMessage(message).withTopicArn(notificationTopicArn));
    }

此 sns 调用失败并出现错误

Invalid parameter: TopicArn (Service: AmazonSNS; Status Code: 400; Error Code: InvalidParameter; Request ID: f320367c-774d-55c0-8b1b-609005851d6c)

我知道问题在于 sns 客户端的区域错误,因为如果我这样做。

@Override
    public void sendNotification(final Notification notification) {
        final String message = notification.toJson();

        //works, but i dont want to set the region everytime
        snsClient.setRegion(Region.getRegion(Regions.US_WEST_2));
         //wrapped in a try catch
         snsClient.publish(new PublishRequest().withMessage(message).withTopicArn(notificationTopicArn));
    }

有用。另外,如果我这样做。

@Override
    public void sendNotification(final Notification notification) {
        final String message = notification.toJson();

       //works, but i want this built as a bean
        snsClient = snsClientBuilder.build()
         //wrapped in a try catch
         snsClient.publish(new PublishRequest().withMessage(message).withTopicArn(notificationTopicArn));
    }

看起来这条线<bean id="snsClient" factory-bean="snsClientBuilder" factory-method="build"/>是行不通的。有任何想法吗?

标签: javaspringamazon-web-services

解决方案


推荐阅读