在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);
            }
        });
    }

}

                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值