Spring 无法为 RabbitMQ 自动装配 ConnectionFactory

问题描述 投票:0回答:4

我正在尝试遵循这个简单的 Spring RabbitMQ 消息传递指南

我有两个自动装配问题:

  1. 无法自动装配。未找到“ConnectionFactory”类型的 beans
  2. 无法自动装配。未找到“RabbitTemplate”类型的 beans。

我找不到我缺少的东西,因为教程指出:

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 rabbitmq
4个回答
1
投票

问题似乎出在 Spring Boot 的版本上。它不会在版本

ConnectionFactory
中自动创建bean
RabbitTemplate
2.7.0
....当我将Spring Boot降级到版本
2.6.8
时,一切正常。

否则,如果您想使用 Spring Boot 2.7.0,您将需要自己创建这些 bean。


1
投票

Spring boot创建这些bean,这是IDEA的问题(如果你使用它)。这是问题


1
投票

问题出在 IDE 上,而不是 Spring 框架的版本上。 我可以确认,因为即使 IntelliJ 抱怨错误,我也能够编译它甚至运行它。 这是 Gradle 构建文件的屏幕截图,其中显示了 Spring boot 和 Java 的版本

IntelliJ 是这么说的

我创建了一个新项目,并将 Boot 降级到 2.6.11 版本,但保留了相同版本的 Java,现在 IntelliJ 一切似乎都正常。

这是 IDE 特定问题。


0
投票

我们有两种 ConnectionFactory 类

  • org.springframework.amqp.rabbit.connection.ConnectionFactory
  • com.rabbitmq.client.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;
}
© www.soinside.com 2019 - 2024. All rights reserved.