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

相关推荐
2501_9414037621 小时前
智能城市公共安全平台中的多语言语法引擎与实时预警实践
rabbitmq
有一个好名字1 天前
Spring AI ——Java开发者的AI集成神器
java·人工智能·spring
e***95641 天前
springboot-自定义注解
java·spring boot·spring
白露与泡影1 天前
spring Security 认证流程闭环与调用链路详解
java·后端·spring
i***58671 天前
Java开发的AI应用框架简述——LangChain4j、Spring AI、Agent-Flex
java·人工智能·spring
i***27951 天前
SpringBoot实现异步调用的方法
java·spring boot·spring
阿在在1 天前
Dubbo 与 Spring 整合全流程解析(含生产者与消费者
java·spring·dubbo
一辉ComeOn1 天前
[源码系列:手写Spring] AOP第二节:JDK动态代理 - 当AOP遇见动态代理的浪漫邂逅
java·后端·spring
b***65321 天前
SpringBoot的@Scheduled和@Schedules有什么区别
java·spring boot·spring
w***4811 天前
CVE-2024-38819:Spring 框架路径遍历 PoC 漏洞复现
java·后端·spring