在springboot中解决RabbitMq的循环依赖问题

文章讲述了在SpringBoot项目中引入RabbitMQ时遇到的循环依赖错误,主要原因是配置类中的自动注入和`@PostConstruct`注解。解决办法包括移除`@Resource`注解、手动注入`RabbitTemplate`并去掉`@PostConstruct`。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在springboot项目中引入RabbitMq导致报循环依赖错误

报错信息

***************************
APPLICATION FAILED TO START
***************************

Description:

The dependencies of some of the beans in the application context form a cycle:

┌─────┐
|  myRabbitConfig
↑     ↓
|  rabbitTemplate defined in class path resource [org/springframework/boot/autoconfigure/amqp/RabbitAutoConfiguration$RabbitTemplateConfiguration.class]
↑     ↓
|  rabbitTemplateConfigurer defined in class path resource [org/springframework/boot/autoconfigure/amqp/RabbitAutoConfiguration$RabbitTemplateConfiguration.class]
└─────┘


Action:

Relying upon circular references is discouraged and they are prohibited by default. Update your application to remove the dependency cycle between beans. As a last resort, it may be possible to break the cycle automatically by setting spring.main.allow-circular-references to true.


Process finished with exit code 1

明显就是循环依赖问题,然后我就去网上搜,大概就是我自己写的配置类引发的这个问题,不能将这个rabbitTemplate自动注入要我们手动注入,并且@PostConstruct这个注解也导致这个问题

解决方法:

删除@Resource注解

自己注入rabbitTemplate

去掉@PostConstruct注解就好了

自定义注入

    @Primary
    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        this.rabbitTemplate = rabbitTemplate;
        rabbitTemplate.setMessageConverter(messageConverter());
        initRabbitTemplate();
        return rabbitTemplate;
    }
 

完整代码

@Configuration
public class MyRabbitConfig {

//    @Resource
    RabbitTemplate rabbitTemplate;

    /**
     * 使用JSON序列化机制,进行消息转换
     * @return
     */
    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }

    @Primary
    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        this.rabbitTemplate = rabbitTemplate;
        rabbitTemplate.setMessageConverter(messageConverter());
        initRabbitTemplate();
        return rabbitTemplate;
    }


    /**
     * 定制RabbitTemplate
     * 1、服务器收到消息就回调
     *      1、publisher-confirm-type: correlated
     *      2、设置确认回调ConfirmCallback
     * 2、消息正确抵达队列进行回调
     *      1、#开启发送端消息抵达队列确认
     *     publisher-returns: true
     *     template:
     *       mandatory: true  #只要抵达队列,以异步模式优先回调我们这个returnConfirm
     *      2、设置确认回调
     * 3、消费端(保证每一个消息被正确消费,此时才可以broken删除这个消息)
     *      1、默认是自动确认的,只要消息接收到,客户端会自动确认
     */
    @PostConstruct //MyRabbitConfig对象创建完以后,调用这个方法
    public void initRabbitTemplate(){
        //设置确认回调
        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
            /**
             *
             * @param correlationData 当前消息的唯一关联数据(这个消息的唯一id)
             * @param b 消息是否成功收到
             * @param s  失败的原因
             */
            @Override
            public void confirm(CorrelationData correlationData, boolean b, String s) {
                System.out.println("confirm...correlationData:"+correlationData+"ack:==="+b+"s====>"+s);

            }
        });

        //设置消息抵达队列的确认回调
        rabbitTemplate.setReturnsCallback(new RabbitTemplate.ReturnsCallback() {
            /**
             * 只要消息没有投递给指定队列,就触发这个失败回调
             * @param returnedMessage
             */
            @Override
            public void returnedMessage(ReturnedMessage returnedMessage) {
                //投递失败的消息,详细信息
                Message message = returnedMessage.getMessage();
                //回复的状态码
                int replyCode = returnedMessage.getReplyCode();
                //回复的文本内容
                String replyText = returnedMessage.getReplyText();
//                当时这个消息发给哪个交换机
                String exchange = returnedMessage.getExchange();
                //当时这个消息用哪个路由键
                String routingKey = returnedMessage.getRoutingKey();

                System.out.println("message====>"+message+"replyCode====>"+replyCode+"replyText=====>"
                        +replyText+"exchange=====>"+exchange+ "routingKey====>"+routingKey);
            }
        });
    }

}

                
### 使用 Spring Boot 搭建配置 RabbitMQ #### 1. 添加依赖项 为了在项目中使用 RabbitMQ,需要在 `pom.xml` 文件中引入必要的依赖项。这些依赖项包含了与 RabbitMQ 进行交互所需的功能库。 ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> ``` 此依赖会自动导入所有必需的组件来支持 AMQP 协议以及 RabbitMQ 的操作[^4]。 #### 2. 启用 RabbitMQ 支持 在启动类上添加 `@EnableRabbit` 注解可以开启基于注解的消息监听器容器的支持。这使得应用能够处理来自 RabbitMQ 的消息事件。 ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.amqp.rabbit.annotation.EnableRabbit; @SpringBootApplication @EnableRabbit // 启用 RabbitMQ public class RabbitMQApplication { public static void main(String[] args) { SpringApplication.run(RabbitMQApplication.class, args); } } ``` 这段代码展示了如何通过简单的几行声明让整个应用程序具备了对 RabbitMQ 的基本访问能力[^1]。 #### 3. 配置 RabbitMQ 参数 接下来,在项目的配置文件 (`application.properties`) 中定义 RabbitMQ 的连接参数: ```properties spring.rabbitmq.host=localhost spring.rabbitmq.port=5672 spring.rabbitmq.username=guest spring.rabbitmq.password=guest ``` 上述设置指定了本地运行的一个默认实例;对于生产环境,则应替换为实际的服务地址和其他安全凭证信息[^3]。 #### 4. 发送和接收消息 利用 `AmqpTemplate` 接口提供的方法可以直接向指定的目标交换机或者队列发送数据包。而要消费消息则可以通过创建带有 `@RabbitListener` 注解的方法实现自动化监听特定主题下的新条目到达通知。 ```java @Autowired private AmqpTemplate rabbitTemplate; // 发送消息到名为 'hello' 的队列 rabbitTemplate.convertAndSend("hello", "Hello from RabbitMQ!"); // 定义一个消费者方法并绑定至某个队列 @RabbitListener(queues = {"hello"}) public void receiveMessage(String message){ System.out.println("Received Message: "+message); } ``` 以上片段说明了怎样简便地完成一次完整的通信循环——既包括发出请求也涵盖了响应逻辑的设计思路[^2].
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值