【问题标题】:Spring unable to autowire ConnectionFactory for RabbitMQSpring 无法为 RabbitMQ 自动装配 ConnectionFactory
【发布时间】:2022-07-19 21:50:31
【问题描述】:

我正在尝试遵循这个简单的guide for Spring RabbitMQ messaging

我有 2 个自动装配问题:

  1. 无法自动接线。找不到“ConnectionFactory”类型的 bean
  2. 无法自动接线。未找到“RabbitTemplate”类型的 bean。

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

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


    【解决方案1】:

    似乎问题出在 Spring Boot 版本中。它不会在版本2.7.0 中自动创建bean ConnectionFactoryRabbitTemplate....当我将Spring Boot 降级到版本2.6.8 时,一切正常。

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-02-06
        • 2017-12-18
        • 2016-09-27
        • 2013-01-29
        • 2023-04-08
        • 2012-12-04
        • 2013-11-12
        • 1970-01-01
        相关资源
        最近更新 更多