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

相关推荐
Joy T3 小时前
Spring AI 项目中的 pom.xml、application.yml 与 Nacos:从依赖管理到运行配置
spring·nacos·pom.xml·profile·参数配置·application.yml
fundoit10 小时前
为什么需要 Access Token 和 ID Token 两个令牌
java·spring·架构·github·oauth2
鱼宵10 小时前
Spring AI 流式输出:Flux + SSE 打字机,回答不再干等三秒
java·人工智能·spring·sse·springai·流式输出
小鹿的周先生11 小时前
第19章-Agent
java·spring·ai
步行cgn12 小时前
MySQL 报错:Access denied for user ‘root‘@‘localhost‘ 详解
java·数据库·spring
鱼宵13 小时前
Spring AI 生产化改造:记忆落 Redis、向量落 ES,重启再也不丢
人工智能·redis·spring·elasticsearch·springai
Wang's Blog13 小时前
Java框架 SpringCloud 快速入门: Nacos 配置热更新
java·spring·spring cloud
天空鸟_时光不老14 小时前
06-给AI流程加一道人工闸门
java·人工智能·spring boot·后端·spring·spring cloud·架构
阿俊-全栈开发15 小时前
LikeShop单商户Java商城如何从容承接高并发流量?
java·开发语言·spring boot·spring·系统架构
天空鸟_时光不老15 小时前
01-我不转Python把AI塞进Java里
java·人工智能·spring boot·后端·spring·spring cloud·架构