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?