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

相关推荐
Boop_wu22 分钟前
[Spring Cloud] 快速上手nacos
后端·spring·spring cloud
GZ_TOGOGO29 分钟前
Spring AI Alibaba 格式化输出
java·人工智能·spring
我登哥MVP1 小时前
SpringCloud Alibaba 核心组件解析:分布式事务(Seata)
java·spring boot·分布式·spring·spring cloud·java-ee·intellij-idea
摇滚侠1 小时前
MyBatis 入门到项目实战 MyBatis 各种查询功能 30-33
java·后端·spring·maven·intellij-idea·mybatis
老马识途2.011 小时前
在AI的帮助下理解spring的启动过程
java·前端·spring
不才不才不不才13 小时前
Spring AI 实战:聊天、提示词、记忆三件套
java·人工智能·spring·ai
椰椰椰耶15 小时前
[SpringCloud][14]OpenFeign参数传递方法
后端·spring·spring cloud
摇滚侠16 小时前
SpringMVC 入门到实战 DispatcherServlet 源码解读 92-95
java·后端·spring·maven·intellij-idea
键盘歌唱家17 小时前
Spring AI 入门分享:它和“直接调 API“到底差在哪
java·人工智能·spring
码不停蹄的玄黓18 小时前
Spring Bean 生命周期
java·后端·spring