Search code examples
springrabbitmq

Spring unable to autowire ConnectionFactory for RabbitMQ


I am trying to follow this simple guide for Spring RabbitMQ messaging.

I have 2 issues with autowiring:

  1. Could not autowire. No beans of 'ConnectionFactory' type found
  2. Could not autowire. No beans of 'RabbitTemplate' type found.

I can't find what I'm missing, because the tutorial states that:

Spring Boot automatically creates a connection factory and a RabbitTemplate, reducing the amount of code you have to write.

Here is my pom.xml in case I'm missing something there:

<?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>

Solution

  • It seems the problem was in version of Spring Boot. It doesn't create beans ConnectionFactory and RabbitTemplate automatically in version 2.7.0....When I downgraded Spring Boot to version 2.6.8, everything worked fine.

    Otherwise in case you want to use Spring Boot 2.7.0, you will need to create these beans yourselves.