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

相关推荐
wuxuanok13 小时前
SpringBoot -原理篇
java·spring boot·spring
若鱼191915 小时前
spring-kafka消费异常处理
spring·kafka
送秋三十五16 小时前
spring源码分析————ListableBeanFactory
java·后端·spring
一又四分之一.16 小时前
spring、springboot、springCloud
spring boot·spring·spring cloud
虫小宝17 小时前
返利app的消息队列架构:基于RabbitMQ的异步通信与解耦实践
分布式·架构·rabbitmq
float_六七17 小时前
Spring事务注解@Transactional核心机制详解
java·后端·spring
Java水解18 小时前
从 “Hello AI” 到企业级应用:Spring AI 如何重塑 Java 生态的 AI 开发
后端·spring
无缘之缘19 小时前
SpringBoot整合RabbitMQ
spring boot·rabbitmq·java-rabbitmq
在线教学养猪19 小时前
Spring Task
java·后端·spring
ChinaRainbowSea20 小时前
9. LangChain4j + 整合 Spring Boot
java·人工智能·spring boot·后端·spring·langchain·ai编程