Spring - RabbitMQ循环依赖问题解决

代码整合消息队列后,启动报错,出现rabbitMQ循环依赖的问题

异常信息:

┌─────┐
|  rabbitTemplate defined in class path resource [org/springframework/boot/autoconfigure/amqp/RabbitAutoConfiguration$RabbitTemplateConfiguration.class]
↑     ↓
|  myRabbitConfig (field org.springframework.amqp.rabbit.core.RabbitTemplate com.xxxx.gulimall.order.config.MyRabbitConfig.rabbitTemplate)
└─────┘

解决方法:

  1. 注释掉 @Autowired和@PostConstruct注解

  2. 自己给容器中放一个RabbitTemplate,参考RabbitAutoConfiguration$RabbitTemplateConfiguration.class

  3. 手动初始化模板方法

    @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 {

    RabbitTemplate rabbitTemplate;

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

    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }

//    @PostConstruct // MyRabbitConfig 对象创建完成以后,执行这个方法
    public void initRabbitTemplate() {
        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
            @Override
            public void confirm(CorrelationData correlationData, boolean b, String s) {
                System.out.println("confirm...correlationData=" + correlationData + ", b=" + b + ", s=" + s);
            }
        });

        rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback() {
            @Override
            public void returnedMessage(@Nullable Message message, int replyCode, @Nullable String replyText, @Nullable String exchange, @Nullable String routingKey) {
                System.out.println("fail Message="+message+",replyCode="+replyCode+",replyText="+replyText +",exchange="+exchange+",routingKey="+routingKey);
            }
        });
    }


}

版权声明:本文为shanghaibao123原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.csdn.net/shanghaibao123/article/details/120347631

相关推荐
White graces6 小时前
正则表达式效验邮箱格式, 手机号格式, 密码长度
前端·spring boot·spring·正则表达式·java-ee·maven·intellij-idea
是姜姜啊!8 小时前
redis的应用,缓存,分布式锁
java·redis·spring
奋斗的袍子0079 小时前
Spring AI + Ollama 实现调用DeepSeek-R1模型API
人工智能·spring boot·深度学习·spring·springai·deepseek
LUCIAZZZ9 小时前
EasyExcel快速入门
java·数据库·后端·mysql·spring·spring cloud·easyexcel
Ase5gqe9 小时前
Spring中的IOC详解
java·后端·spring
黄名富10 小时前
Spring Cloud — 深入了解Eureka、Ribbon及Feign
分布式·spring·spring cloud·微服务·eureka·ribbon
m0_7482482311 小时前
Spring Framework 中文官方文档
java·后端·spring
先睡11 小时前
Spring MVC的基本概念
java·spring·mvc
LUCIAZZZ12 小时前
SkyWalking快速入门
java·后端·spring·spring cloud·微服务·springboot·skywalking
m0_7482451712 小时前
SpringCloud-使用FFmpeg对视频压缩处理
spring·spring cloud·ffmpeg