MyRabbitConfig设置回调提示循环依赖如何解决

@Configuration
public class MyRabbitConfig {
    @Autowired
    RabbitTemplate rabbitTemplate;
    @PostConstruct 
    public void initRabbitTemplate(){

        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
            @Override
            public void confirm(CorrelationData correlationData, boolean ack, String cause) {
            }
        });


        rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback() {

            @Override
            public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
            }
        });
    }
}
为什么会报错提示循环依赖呢

答:

问题出在@PostConstruct注解上。@PostConstruct注解用于在依赖注入完成后执行方法,但如果在同一个类中使用了该注解,就会导致循环依赖的问题。

循环依赖指的是两个对象之间互相依赖,如果其中一个对象依赖另一个对象,而另一个对象也依赖这个对象,就会形成一个循环依赖的链条。

在这个例子中,MyRabbitConfig类中使用了@PostConstruct注解,并在其中调用了initRabbitTemplate()方法。但是,initRabbitTemplate()方法中也调用了rabbitTemplate属性,而rabbitTemplate属性又依赖于MyRabbitConfig类。这就导致了循环依赖的问题。

为了解决这个问题,可以将initRabbitTemplate()方法移动到另一个类中,并在该类中使用@PostConstruct注解。这样就可以避免循环依赖的问题了。

或者

取消RabbitTemplate  @Autowired  及  initRabbitTemplate  @PostConstruct ,增加如下代码,自己造一个Bean

@Primary
@Bean
public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory){
    RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
    this.rabbitTemplate = rabbitTemplate;
    rabbitTemplate.setMessageConverter(messageConverter());
    initRabbitTemplate();
    return rabbitTemplate;
}
在 Spring Boot 中使用 RabbitMQ 拉模式消费消息,需要进行以下步骤: 1. 引入 RabbitMQ 依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> ``` 2. 配置 RabbitMQ 连接信息: ```properties spring.rabbitmq.host=localhost spring.rabbitmq.port=5672 spring.rabbitmq.username=guest spring.rabbitmq.password=guest ``` 3. 创建消息监听器: ```java @Component public class MyMessageListener { @Autowired private RabbitTemplate rabbitTemplate; @RabbitListener(queues = "my_queue") public void onMessage() { Message message = rabbitTemplate.receive("my_queue"); if (message != null) { String body = new String(message.getBody(), StandardCharsets.UTF_8); System.out.println("Received message: " + body); } } } ``` 4. 配置消息监听器容器: ```java @Configuration public class MyRabbitConfig { @Autowired private ConnectionFactory connectionFactory; @Autowired private MyMessageListener myMessageListener; @Bean public SimpleMessageListenerContainer messageListenerContainer() { SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(); container.setConnectionFactory(connectionFactory); container.setQueueNames("my_queue"); container.setMessageListener(myMessageListener); return container; } } ``` 以上就是使用 RabbitMQ 拉模式消费消息的步骤,需要注意的是,拉模式消费消息需要手动调用 `RabbitTemplate.receive()` 方法获取消息。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值