java - 工厂方法在 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"/>
是行不通的。有任何想法吗?
解决方案
推荐阅读
- openshift - 如何在 Centos 7 上重新启动 Openshift Origin master?
- python - for-loop 中子图的特定标题重复最后一个子图
- wordpress - 如何为“WordPress”添加特定天数(+1 天)到(ISO 和 RFC 格式)的当前日期?
- android - Android NotificationListener取消通知不起作用
- entity-framework-core - 如果您尝试包含的属性为 null,则在 Entity Framework Core 2.0 中调用 Include 会使实体为 null
- macos - 通过自制软件安装的任何东西都会抛出“找不到命令”
- sql - Sql select - 如果为空,如何从其他表中选择
- bash - 文件消失而不是重命名
- node.js - 找不到模块'source-map-support/register
- python - Keras:模型预测,检查输入形状时出错