0

2020欧洲杯时间表I am trying to configure RabbitMQ with Spring Boot. Below is a snapshot of my config class.

Case 1:

   @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate template = new RabbitTemplate(connectionFactory);
        template.setMessageConverter(new Jackson2JsonMessageConverter());            
        return template;
    }

This code works fine.

Code 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;
        }

In this case however, code works fine if I use the Line 1, where I am creating object of Jackson2JsonMessageConverter.

But I am writing this code to understand the working of an already existing code where instead of line 1, line 2 is used. So, when I use line 2 instead of line 1, I get error:

2020欧洲杯时间表Consider defining a bean of type 'org.springframework.amqp.support.converter.MessageConverter' in your configuration.

So I have 2 questions:

  1. Why this error?
  2. If I define a bean for MessageConverter, say

    @Bean public MessageConverter createMessageConverter() { return new Jackson2JsonMessageConverter(); }

2020欧洲杯时间表then it works. Then why is it not asking me to define a bean for ConnectionFactory argument?

2020欧洲杯时间表PS: There are no @Autowired used, neither here, nor in the code I am trying to understand and both arguments, ConnectionFactory & MessageConverter are interfaces and not classes

2

The short answer is: You need to understand the concept of AutoConfiguratrions in Spring Boot, which will create a lot of @Beans for you, without you "seeing them".

A very good article to understand AutoConfigurations is this:

In your case, you also might want to have a look at the "RabbitAutoConfiguration" class from Spring Boot's source code.

| improve this answer | |
  • Hey man... I am a YouTube subscriber... Great videos... Is this concept explained more in detail in your Spring Boot videos? – Rajyawardhan Singh Panwar Mar 31 at 10:48
  • 1
    Thank you :) I didn't put all my Spring Boot videos onto YouTube, there is a talk of me explaining it - but it is in German. Give the article a try, it tries to explain it in really simple ways and is the most up-to-date. Hope that helps! – Marco Behler Mar 31 at 11:45
  • I read the article. Cleared my doubts. There is, in fact, a parent spring-boot project whose configuration contains the definition of MessageConverter Bean. This project is added to pom and its configuration is included in current project's config using @Import. Thanks a lot. – Rajyawardhan Singh Panwar Mar 31 at 13:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.