Parameter 0 of method listenerAdapter in "package/class" required a bean of type 'com.example.messagingrabbitmq.Receiver' that could not be found

huangapple go评论79阅读模式
英文:

Parameter 0 of method listenerAdapter in "package/class" required a bean of type 'com.example.messagingrabbitmq.Receiver' that could not be found

问题

抱歉,我只能提供代码的翻译,以下是您提供的代码的翻译部分:

MessagingRabbitmqApplication 类:

package com.example.messagingrabbitmq;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class MessagingRabbitmqApplication {

    static final String topicExchangeName = "spring-boot-exchange";

    static final String queueName = "spring-boot";

    @Bean
    Queue queue() {
        return new Queue(queueName, false);
    }

    @Bean
    TopicExchange exchange() {
        return new TopicExchange(topicExchangeName);
    }

    @Bean
    Binding binding(Queue queue, TopicExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("foo.bar.#");
    }

    @Bean
    SimpleMessageListenerContainer container(ConnectionFactory connectionFactory,
                                             MessageListenerAdapter listenerAdapter) {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.setQueueNames(queueName);
        container.setMessageListener(listenerAdapter);
        return container;
    }

    @Bean
    MessageListenerAdapter listenerAdapter(Receiver receiver) {
        return new MessageListenerAdapter(receiver, "receiveMessage");
    }

    public static void main(String[] args) throws InterruptedException {
        SpringApplication.run(MessagingRabbitmqApplication.class, args).close();
    }
}

Receiver 类:

package com.example.messagingrabbitmq;

import java.util.concurrent.CountDownLatch;
import org.springframework.stereotype.Component;

@Component
public class Receiver {

    private CountDownLatch latch = new CountDownLatch(1);

    public void receiveMessage(String message) {
        System.out.println("Received <" + message + ">");
        latch.countDown();
    }

    public CountDownLatch getLatch() {
        return latch;
    }
}

Runner 类:

package com.example.messagingrabbitmq;

import java.util.concurrent.TimeUnit;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class Runner implements CommandLineRunner {

    private final RabbitTemplate rabbitTemplate;
    private final Receiver receiver;

    public Runner(Receiver receiver, RabbitTemplate rabbitTemplate) {
        this.receiver = receiver;
        this.rabbitTemplate = rabbitTemplate;
    }

    @Override
    public void run(String... args) throws Exception {
        System.out.println("Sending message...");
        rabbitTemplate.convertAndSend(MessagingRabbitmqApplication.topicExchangeName, "foo.bar.baz", "Hello from RabbitMQ!");
        receiver.getLatch().await(10000, TimeUnit.MILLISECONDS);
    }
}

至于您提到的错误问题以及如何设置应用程序的属性文件,我无法提供帮助,因为这些需要更多的上下文和实际的配置文件内容。如果您有更具体的问题,可以提出来,我会尽力回答。

英文:

Hеllo, all,

I am building a simple RabbitMQ AMQP server with Spring/Docker to run the server itself and I am following this guide: https://spring.io/guides/gs/messaging-rabbitmq/#scratch

The classes are literally copied from there.

I have 3 classes in src/main/java/com.example.messagingrabbitmq and they are the following:

MessagingRabbitmqApplication:

package com.example.messagingrabbitmq;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class MessagingRabbitmqApplication {
static final String topicExchangeName = &quot;spring-boot-exchange&quot;;
static final String queueName = &quot;spring-boot&quot;;
@Bean
Queue queue() {
return new Queue(queueName, false);
}
@Bean
TopicExchange exchange() {
return new TopicExchange(topicExchangeName);
}
@Bean
Binding binding(Queue queue, TopicExchange exchange) {
return BindingBuilder.bind(queue).to(exchange).with(&quot;foo.bar.#&quot;);
}
@Bean
SimpleMessageListenerContainer container(ConnectionFactory connectionFactory,
MessageListenerAdapter listenerAdapter) {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.setQueueNames(queueName);
container.setMessageListener(listenerAdapter);
return container;
}
@Bean
MessageListenerAdapter listenerAdapter(Receiver receiver) {
return new MessageListenerAdapter(receiver, &quot;receiveMessage&quot;);
}
public static void main(String[] args) throws InterruptedException {
SpringApplication.run(MessagingRabbitmqApplication.class, args).close();
}
}

Receiver:

package com.example.messagingrabbitmq;
import java.util.concurrent.CountDownLatch;
import org.springframework.stereotype.Component;
@Component
public class Receiver {
private CountDownLatch latch = new CountDownLatch(1);
public void receiveMessage(String message) {
System.out.println(&quot;Received &lt;&quot; + message + &quot;&gt;&quot;);
latch.countDown();
}
public CountDownLatch getLatch() {
return latch;
}
}

Runner:

package com.example.messagingrabbitmq;
import java.util.concurrent.TimeUnit;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
@Component
public class Runner implements CommandLineRunner {
private final RabbitTemplate rabbitTemplate;
private final Receiver receiver;
public Runner(Receiver receiver, RabbitTemplate rabbitTemplate) {
this.receiver = receiver;
this.rabbitTemplate = rabbitTemplate;
}
@Override
public void run(String... args) throws Exception {
System.out.println(&quot;Sending message...&quot;);
rabbitTemplate.convertAndSend(MessagingRabbitmqApplication.topicExchangeName, &quot;foo.bar.baz&quot;, &quot;Hello from RabbitMQ!&quot;);
receiver.getLatch().await(10000, TimeUnit.MILLISECONDS);
}
}

I tried to run the program from the MessagingRabbitmqApplication java class, but resulted in the following error:

Description:
Parameter 0 of method listenerAdapter in com.example.messagingrabbitmq.MessagingRabbitmqApplication required a bean of type &#39;com.example.messagingrabbitmq.Receiver&#39; that could not be found.
Action:
Consider defining a bean of type &#39;com.example.messagingrabbitmq.Receiver&#39; in your configuration.
Process finished with exit code 1

I tried to create a Receiver Bean in Receiver.java class with the following, but it resulted in the same error:

   @Bean
Receiver receiver() {
return new Receiver();
}

I also have a question on how to set up my application.properties file so the Queues/Exchanges can be set up in my running Docker container? It is currently running on 'localhost:15672'.

Thank you very much for the help in advance.

答案1

得分: 1

Update:

我检查了你的现有代码,它运行正常。

TestAmqpApplication:

package com.example.testamqp;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class TestAmqpApplication {

    static final String topicExchangeName = "spring-boot-exchange";

    static final String queueName = "spring-boot";

    @Bean
    Queue queue() {
        return new Queue(queueName, false);
    }

    @Bean
    TopicExchange exchange() {
        return new TopicExchange(topicExchangeName);
    }

    @Bean
    Binding binding(Queue queue, TopicExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("foo.bar.#");
    }

    @Bean
    SimpleMessageListenerContainer container(ConnectionFactory connectionFactory,
                                             MessageListenerAdapter listenerAdapter) {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.setQueueNames(queueName);
        container.setMessageListener(listenerAdapter);
        return container;
    }

    @Bean
    MessageListenerAdapter listenerAdapter(Receiver receiver) {
        return new MessageListenerAdapter(receiver, "receiveMessage");
    }

    public static void main(String[] args) {
        SpringApplication.run(TestAmqpApplication.class, args);
    }

}

Receiver:

package com.example.testamqp;

import org.springframework.stereotype.Component;

import java.util.concurrent.CountDownLatch;

@Component
public class Receiver {

    private CountDownLatch latch = new CountDownLatch(1);

    public void receiveMessage(String message) {
        System.out.println("Received <" + message + ">");
        latch.countDown();
    }

    public CountDownLatch getLatch() {
        return latch;
    }

}

Runner:

package com.example.testamqp;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class Runner implements CommandLineRunner {

    private final RabbitTemplate rabbitTemplate;
    private final Receiver receiver;

    public Runner(Receiver receiver, RabbitTemplate rabbitTemplate) {
        this.receiver = receiver;
        this.rabbitTemplate = rabbitTemplate;
    }

    @Override
    public void run(String... args) throws Exception {
        System.out.println("Sending message...");
        rabbitTemplate.convertAndSend(TestAmqpApplication.topicExchangeName, "foo.bar.baz", "Hello from RabbitMQ!");
        receiver.getLatch().await(10000, TimeUnit.MILLISECONDS);
    }

}

控制台输出的截图:

Parameter 0 of method listenerAdapter in "package/class" required a bean of type 'com.example.messagingrabbitmq.Receiver' that could not be found

请使用Maven正确编译你的项目。再次执行Maven的cleaninstall阶段。

如果你仍然面临问题,请执行以下步骤:

  • 同时,在IntelliJ中为项目使缓存失效。
    Parameter 0 of method listenerAdapter in "package/class" required a bean of type 'com.example.messagingrabbitmq.Receiver' that could not be found

  • 最后但并非最不重要的,创建一个新的项目并再次尝试。

英文:

Update:

I checked your existing code, it's working fine.

TestAmqpApplication :

package com.example.testamqp;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class TestAmqpApplication {
static final String topicExchangeName = &quot;spring-boot-exchange&quot;;
static final String queueName = &quot;spring-boot&quot;;
@Bean
Queue queue() {
return new Queue(queueName, false);
}
@Bean
TopicExchange exchange() {
return new TopicExchange(topicExchangeName);
}
@Bean
Binding binding(Queue queue, TopicExchange exchange) {
return BindingBuilder.bind(queue).to(exchange).with(&quot;foo.bar.#&quot;);
}
@Bean
SimpleMessageListenerContainer container(ConnectionFactory connectionFactory,
MessageListenerAdapter listenerAdapter) {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.setQueueNames(queueName);
container.setMessageListener(listenerAdapter);
return container;
}
@Bean
MessageListenerAdapter listenerAdapter(Receiver receiver) {
return new MessageListenerAdapter(receiver, &quot;receiveMessage&quot;);
}
public static void main(String[] args) {
SpringApplication.run(TestAmqpApplication.class, args);
}
} 

Receiver:

package com.example.testamqp;
import org.springframework.stereotype.Component;
import java.util.concurrent.CountDownLatch;
@Component
public class Receiver {
private CountDownLatch latch = new CountDownLatch(1);
public void receiveMessage(String message) {
System.out.println(&quot;Received &lt;&quot; + message + &quot;&gt;&quot;);
latch.countDown();
}
public CountDownLatch getLatch() {
return latch;
}
}

Runner:

package com.example.testamqp;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;
@Component
public class Runner implements CommandLineRunner {
private final RabbitTemplate rabbitTemplate;
private final Receiver receiver;
public Runner(Receiver receiver, RabbitTemplate rabbitTemplate) {
this.receiver = receiver;
this.rabbitTemplate = rabbitTemplate;
}
@Override
public void run(String... args) throws Exception {
System.out.println(&quot;Sending message...&quot;);
rabbitTemplate.convertAndSend(TestAmqpApplication.topicExchangeName, &quot;foo.bar.baz&quot;, &quot;Hello from RabbitMQ!&quot;);
receiver.getLatch().await(10000, TimeUnit.MILLISECONDS);
}
}

Screenshot of the console output:

Parameter 0 of method listenerAdapter in "package/class" required a bean of type 'com.example.messagingrabbitmq.Receiver' that could not be found

Compile your project with maven properly. Do Maven clean and install phases again.

If you are still facing issue, then do this steps:

  • Also, invalidate cache for the project in Intellij.
    Parameter 0 of method listenerAdapter in "package/class" required a bean of type 'com.example.messagingrabbitmq.Receiver' that could not be found

  • Last but not the least, create a fresh project and try again.

huangapple
  • 本文由 发表于 2023年3月3日 20:54:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75627344.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定