0

I don't think this is a spring-amqp-specific question, but it provides a relatively tidy example to motivate my question. Consider this @Configuration, from the spring-rabbit Hello World project:

@Configuration
public class HelloWorldConfiguration {
    …

    @Bean /* <-- Optional?! What's the point? */
    public ConnectionFactory connectionFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory("localhost");
        return connectionFactory;
    }

    @Bean
    public AmqpAdmin amqpAdmin() {
        return new RabbitAdmin(connectionFactory());
    }

    @Bean
    public RabbitTemplate rabbitTemplate() {
        RabbitTemplate template = new RabbitTemplate(connectionFactory());
        …
    }

    @Bean
    // Every queue is bound to the default direct exchange
    public Queue helloWorldQueue() {...}
}

I can easily conceive why the AmqpAdmin function produces a bean, and as such the Queue object seems completely reasonable. It seems to me, though, that rabbitTemplate and connectionFactory could be regular non-bean functions. Even static functions, very simple animals.

What is the @Bean annotation adding to these functions? What dependency-injection patterns does it facilitate?


Answers like the below are everywhere. These assume I know I want a bean, and deal with why it is I'm not getting one. My question here is different: Why do I need these things to be a bean? Isn't the undecorated function equivalent?

Spring, working with @Configuration and @Bean annotations

Community
  • 1
  • 1
Andres Jaan Tack
  • 22,566
  • 11
  • 59
  • 78
  • Apart of this, I just wrote *java @bean @configuration* on my seo and there are several posts onlline about this topic. – Luiggi Mendoza Jan 23 '15 at 16:15
  • 1
    If you want to be able to have the rabbitTemplate injected in your own beans, it must be a bean. Regarding the connectionFactory, even if you don't want it injected in your own beans, you want the same instance injected used in the admin and in the template. Making it a singleton bean makes it easy. – JB Nizet Jan 23 '15 at 16:18
  • Aha! @JBNizet: It looks in this code like I would actually create two instances of `ConnectionFactory` -- one for the template, one for the AmqpAdmin. Is that not the case? How can that be when it's clearly calling the function twice? – Andres Jaan Tack Jan 23 '15 at 16:45
  • 1
    @LuiggiMendoza the google results tend to explain "These annotations will produce the following XML," assuming that's helpful to me. It isn't -- I want to know what the product of the configuration is. Similarly, the Spring documentation assumes I want to have beans. Do I? Why? Isn't the undecorated function the same here? – Andres Jaan Tack Jan 23 '15 at 16:47
  • @AndresJaanTack `@Bean` methods are magic. See http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#beans-java-further-information-java-config – JB Nizet Jan 23 '15 at 20:20
  • @JBNizet that constitutes a great answer to my question, actually. Write it! Let me give you rep! :) – Andres Jaan Tack Jan 24 '15 at 12:06
  • I may not answer a closed question. You had a useful answer as a comment, and I have plenty or rep points already, so don't worry about that – JB Nizet Jan 24 '15 at 12:08
  • @LuiggiMendoza Can you revisit this question, in light of the clarity we've achieved here? – Andres Jaan Tack Jan 24 '15 at 12:18

0 Answers0