@EnableRabbit turns on annotation-driven RabbitMQ listeners in Spring AMQP: it registers the post-processor that finds @RabbitListener methods and wires them to listener containers. In a Spring Boot application with spring-boot-starter-amqp you do not need to add it, because Boot's auto-configuration applies it for you. You only add it yourself in plain Spring (non-Boot) applications.
It does not connect to the broker or declare queues by itself. Connections, queues and templates come from separate beans or Boot properties.
@EnableRabbit does@EnableRabbit without Spring Boot@EnableRabbit doesWhen Spring processes @EnableRabbit it imports RabbitBootstrapConfiguration, which registers a RabbitListenerAnnotationBeanPostProcessor and a RabbitListenerEndpointRegistry. At startup the post-processor scans beans for @RabbitListener and @RabbitHandler methods and asks a RabbitListenerContainerFactory (by default the bean named rabbitListenerContainerFactory) to create a message listener container for each one. The containers consume messages from the queues and call your methods.
So the annotation enables:
@RabbitListener on methods and @RabbitHandler on methods of a class-level listener.@EnableRabbit is the Spring AMQP counterpart of @EnableJms and @EnableKafka.
No. Spring Boot's RabbitAutoConfiguration creates the ConnectionFactory, RabbitTemplate, AmqpAdmin and a SimpleRabbitListenerContainerFactory, and enables annotation-driven listeners automatically when Spring AMQP is on the classpath. The configuration comes from spring.rabbitmq.* properties:
Adding @EnableRabbit on a Boot application is harmless but redundant. Use it when you define your own container factory in a non-Boot application or want to be explicit.
Dependency:
Declare a queue, a producer and a consumer:
Boot's RabbitAdmin declares the Queue bean on the broker when the connection opens, so the queue exists before the listener starts.
@EnableRabbit without Spring BootIn a plain Spring application you declare the infrastructure yourself and add the annotation:
The bean name rabbitListenerContainerFactory matters: @RabbitListener uses it unless you set containerFactory = "otherFactory" on the annotation.
auto, a message is acknowledged when the listener returns normally. If the method throws, the message is rejected and, by default, requeued, which can create an endless redelivery loop. Set spring.rabbitmq.listener.simple.default-requeue-rejected=false and configure a dead-letter exchange for poison messages.spring.rabbitmq.listener.simple.retry.enabled=true adds stateless retries with backoff before the message is rejected.@EnableRabbit required in Spring Boot?No. Auto-configuration enables annotation-driven listeners when spring-boot-starter-amqp is present.
@RabbitListener method never called?Typical causes are a missing @EnableRabbit in a non-Boot application, a class that is not a Spring bean, a queue name that does not exist on the broker, or a listener container factory with a different bean name.
@RabbitListener and @RabbitHandler?@RabbitListener marks a method (or a class) as a message consumer. @RabbitHandler selects among several methods in a class-level listener based on the payload type.
@EnableRabbit create queues?No. Queues, exchanges and bindings are declared as Queue, Exchange and Binding beans, and RabbitAdmin creates them on the broker.
@EnableRabbit activates @RabbitListener processing in Spring AMQP. Spring Boot applies it automatically, so you need it only in non-Boot setups. Declare your queues as beans, choose acknowledgement and retry settings deliberately, and use a dead-letter queue for messages that keep failing.