0

I have a simple question but I can't find anything about it in the documentation nor any example.

I have a Spring Boot project which uses RabbitMQ and the Spring boot AMQP module. I'm using a specific exchange, and the Spring Jackson2JsonMessageConverter so I need to define my own RabbitTemplate bean, I guess (at this point, if anyone knows if it can be done another simpler way, you are welcome).

@Bean
RabbitTemplate myAwesomeTemplate() {
    RabbitTemplate rabbitTemplate = new RabbitTemplate();
    rabbitTemplate.setExchange(myBeautifulExchange);
    rabbitTemplate.setRoutingKey("legendary");
    rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
    rabbitTemplate.setConnectionFactory(connectionFactory());
    return rabbitTemplate;
}

The real question for me here is that I have to define a CachingConnectionFactory bean too, just to set it to the template, but it seems a CachingConnectionFactory is already defined by the Spring RabbitAutoConfiguration class, so I was wondering if I could simply do this in my class:

@Autowired
CachingConnectionFactory cachingConnectionFactory

But Intellij keeps complaining because it cannot find the definition of that bean, as if it was some definition order matter or something... When I launch the project that way, it seems ok anyway, but I don't like when Intellij complains, it's like I'm doing something "not the standard way".

Thank you everyone in advance ! Sorry for the long explanation.

PS: same problem if I just want to autowire the Spring RabbitProperties bean and use the host, username and password properties (respectively spring.rabbitmq.host, spring.rabbitmq.username and spring.rabbitmq.password).

Lebowski
  • 588
  • 7
  • 21

1 Answers1

0

But Intellij keeps complaining because it cannot find the definition ... I don't like when Intellij complains, it's like I'm doing something "not the standard way".

You could try to fool it by adding it to the factory method and using argument injection...

@Bean
RabbitTemplate myAwesomeTemplate(ConnectionFactory rabbitConnectionFactory) {
    ...
}

Same thing with properties.

Either way (@Autowired or this one) is "the standard way".

If it still complains, I suggest you talk to JetBrains.

Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • So you think it should work this way ? I already tried to inject it in the bean factory method, but ...same issue, Intellij complains the same way on the method parameter... – Lebowski Mar 09 '16 at 14:48
  • That's the way the framework's designed to be used; I can't speak to the IntelliJ support for it; sorry. – Gary Russell Mar 09 '16 at 14:53
  • No problem ! Thank you for the explanation. At least I know now that I'm not doing anything wrong here. – Lebowski Mar 10 '16 at 09:59