私はAWSにメッセージを送信するSqsQueueSenderを持っています。私はこのクラスをテストしたい。私の考えは、それが必要なクラスに注入されるのは@Component
でなければならないということです。重要なことは、私はSqsQueueSender
のエンドポイントを、テスト環境とプロダクション環境で異なるものにしたいと考えています。@Componentクラスを適切に設定する方法
私は、@Autowiredと@Componentをさまざまな方法でクラスの周りを移動してきましたが、いくつかの基本的な誤解が必要です。
package com.foo.fulfillmentApi.dao;
import com.amazonaws.services.sqs.AmazonSQSAsyncClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.aws.messaging.core.QueueMessagingTemplate;
import org.springframework.messaging.support.MessageBuilder;
@Component
public class SqsQueueSender {
private static final Logger LOGGER = LoggerFactory.getLogger(SqsQueueSender.class);
private final QueueMessagingTemplate queueMessagingTemplate;
@Autowired
AmazonSQSAsyncClient amazonSQSAsyncClient;
//This value is from /resources/application.properties
private @Value("${sqs.endpoint}") String endpoint;
public SqsQueueSender(AmazonSQSAsyncClient amazonSqsAsyncClient) {
amazonSqsAsyncClient.setEndpoint(endpoint);
this.queueMessagingTemplate = new QueueMessagingTemplate(amazonSqsAsyncClient);
}
public void send(String queueName, String message) {
this.queueMessagingTemplate.convertAndSend(queueName, MessageBuilder.withPayload(message).build());
}
}
起動状態にエラーメッセージによって引き起こさ
: org.springframework.beans.factory.NoSuchBeanDefinitionException:タイプ なし 予選Beanのcom.amazonawsここでは私の最新の設定です。 services.sqs.AmazonSQSAsyncClient 'available:expected autowire候補となる少なくとも1つのbean。依存関係 注釈:{}
あなたがAmazonSQSAsyncClient
を渡す必要がありSqsQueueSender
を実装します。このコンポーネントがそのタイプの既存のBeanにアクセスできるようにするにはどうすればよいですか?
awsについて何も知らない。しかし、 'AmazonSQSAsyncClient'定義はどこのbeanですか? – Patrick
あなたの質問が分かっていれば、そのクラスは私のプロジェクトで '@ Bean'として定義されていません。インポートした既存のクラスから@Beanを作成するにはどうすればよいですか? –