私は春のブートでのRabbitMQを設定しようとしています。以下は、私のconfigクラスのスナップショットです。
ケース1:
@Bean
public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
RabbitTemplate template = new RabbitTemplate(connectionFactory);
template.setMessageConverter(new Jackson2JsonMessageConverter());
return template;
}
このコードは、罰金に動作します。
コード2:
@Bean
public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory, MessageConverter messageConverter) {
RabbitTemplate template = new RabbitTemplate(connectionFactory);
// template.setMessageConverter(new Jackson2JsonMessageConverter()); // Line 1 - works
// template.setMessageConverter(messageConverter); // Line 2 - error: asks to inject Bean
return template;
}
私はJackson2JsonMessageConverterのオブジェクトを作成していますライン1を、使用している場合しかし、この場合、コードは、罰金に動作します。
しかし、私は代わりにライン1の2行目が使用されている既存のコードの動作を理解するために、このコードを書いています。私はライン2の代わりに、ライン1を使用するときに、私はエラーを取得します:
ご使用の構成にタイプ「org.springframework.amqp.support.converter.MessageConverter」のBeanを定義することを検討してください。
だから私は2つの質問があります:
- なぜ、このエラー?
私はMessageConverterためのBeanを定義する場合は、言います
@Bean公共MessageConverter createMessageConverter(){リターン新しいJackson2JsonMessageConverter()。}
それは動作します。そして、なぜそれがConnectionFactoryの引数のBeanを定義するために私を求めていませんか?
PS:何@Autowiredはありません使用、どちらもここで、また私が理解しようとしているコードにし、両方の引数、ConnectionFactoryの&MessageConverterはインターフェースではなくクラスであります
短い答えは:あなたは「彼らを見て」あなたなしでは、あなたのための@Beansの多くを作成する春ブーツでAutoConfiguratrions、の概念を理解する必要があります。
AutoConfigurationsを理解するために非常に良い記事がこれです:
https://www.marcobehler.com/guides/spring-boot
あなたのケースでは、あなたはまた、春ブーツのソースコードから「RabbitAutoConfiguration」クラスを見てしたい場合があります。