tanvi:
私は、SNSにアクセスするためにシングルトンAmazonSNSを維持しようとしています。私はSNS(一つだけSNSが現在追加される)と、メッセージを公開するアクセサのためのモジュールを作成しました。次のように私のコードは次のとおりです。
public class SNSModule extends AbstractModule {
@Override
protected void configure() {
}
@Provides
@Named("PSSNSRegionName")
private Regions getPSSNSRegionName(
@Named(BeanConstants.P_S_SNS_REGION) final String regionName) {
return Regions.fromName(regionName);
}
@Provides
@Singleton
@Named(BeanConstants.P_S_SNS)
public AmazonSNS getPSSNS(
@NonNull @Named("PaymentSuccessSNSRegionName") final Regions region,
final Config config) {
return AmazonSNSClientBuilder.standard()
.withCredentials(new AWSCredentialsProviderImpl(config.getSnsMaterialSet()))
.withRegion(region)
.build();
}
}
次のようにSNSアクセサは次のようになります。
@RequiredArgsConstructor(access = AccessLevel.PUBLIC, onConstructor = @__(@Inject))
public class SNSAccessor {
@Named(BeanConstants.P_S_SNS)
private final AmazonSNS snsClient;
private static final String COLON = ":";
private static final short ARN_LENGTH = 6;
public PublishResult publishToSNS(@NonNull final String snsTopicArn, @NonNull final String messageToPublish) {
try {
String[] arnParts = snsTopicArn.split(COLON);
Preconditions.checkArgument(
snsTopicArn.split(COLON).length == ARN_LENGTH,
"Expected arn to have 6 parts but found: " + arnParts.length
);
return snsClient.publish(snsTopicArn, messageToPublish);
} catch (InternalErrorException e) {
log.error("InternalErrorException publishing notification to SNS", e);
throw new RetriableException(e);
} catch (Exception e) {
log.error("Exception publishing notification to SNS", e);
throw new NonRetriableException(e);
}
}
}
私はパッケージをビルドすることができましたが、それは、実行時にConfigurationExceptionがを投げました。
Caused by: com.google.inject.ConfigurationException: Guice configuration errors:
1) No implementation for com.amazonaws.services.sns.AmazonSNS was bound.
while locating com.amazonaws.services.sns.AmazonSNS
for parameter 0 at com.xyz.service.sns.SNSAccessor.<init>(SNSAccessor.java:29)
あなたは私が私が間違ってやっているかを把握助けを喜ばせるだろうか?私は正しく、メインでSNSModuleをインストールしました。
ディーパック・グプタ:
ロンボク島の@RequiredArgsConstructor
持つonConstructor
オプションは、注釈付きの依存性の注入では動作しません。あなたは、このような場合のために、明示的にコンストラクタを記述する必要があります。