我正在尝试遵循这个简单的 Spring RabbitMQ 消息传递指南。
我有两个自动装配问题:
我找不到我缺少的东西,因为教程指出:
Spring Boot 自动创建连接工厂和 RabbitTemplate,减少您必须编写的代码量。
这是我的 pom.xml,以防我遗漏了一些东西:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>RabbitMQDemoApp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>RabbitMQDemoApp</name>
<description>RabbitMQDemoApp</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
问题似乎出在 Spring Boot 的版本上。它不会在版本
ConnectionFactory
中自动创建beanRabbitTemplate
和2.7.0
....当我将Spring Boot降级到版本2.6.8
时,一切正常。
否则,如果您想使用 Spring Boot 2.7.0,您将需要自己创建这些 bean。
Spring boot创建这些bean,这是IDEA的问题(如果你使用它)。这是问题。
我们有两种 ConnectionFactory 类
一定要使用第一个
@Bean("rabbitListenerContainerFactory")
public RabbitListenerContainerFactory<?> rabbitFactory
(org.springframework.amqp.rabbit.connection.ConnectionFactory connectionFactory) {
var factory = new SimpleRabbitListenerContainerFactory();
factory.setConnectionFactory(connectionFactory);
factory.setMessageConverter(new Jackson2JsonMessageConverter());
return factory;
}