我尝试使用@RabbitListener自动创建和绑定exchange和queue,但是它抛出异常
2023-02-19 16:56:58.040 ERROR 23348 --- [ main] o.s.boot.SpringApplication : Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'certificateApplyEventHandler' defined in file [E:\codes\trace-platform\supervise\core\target\classes\tp\adapter\event\handler\CertificateApplyEventHandler.class]: Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: Exchange name required; binding queue EnterpriseCertificateApplyEvent
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:603) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:879) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878) ~[spring-context-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) ~[spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) ~[spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) ~[spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) ~[spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) ~[spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) ~[spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
at tp.TracePlatformApplication.main(TracePlatformApplication.java:11) ~[classes/:na]
Caused by: java.lang.IllegalArgumentException: Exchange name required; binding queue EnterpriseCertificateApplyEvent
at org.springframework.util.Assert.isTrue(Assert.java:136) ~[spring-core-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.amqp.rabbit.annotation.RabbitListenerAnnotationBeanPostProcessor.declareExchangeAndBinding(RabbitListenerAnnotationBeanPostProcessor.java:651) ~[spring-rabbit-2.2.1.RELEASE.jar:2.2.1.RELEASE]
at org.springframework.amqp.rabbit.annotation.RabbitListenerAnnotationBeanPostProcessor.registerBeansForDeclaration(RabbitListenerAnnotationBeanPostProcessor.java:620) ~[spring-rabbit-2.2.1.RELEASE.jar:2.2.1.RELEASE]
at org.springframework.amqp.rabbit.annotation.RabbitListenerAnnotationBeanPostProcessor.resolveQueues(RabbitListenerAnnotationBeanPostProcessor.java:582) ~[spring-rabbit-2.2.1.RELEASE.jar:2.2.1.RELEASE]
at org.springframework.amqp.rabbit.annotation.RabbitListenerAnnotationBeanPostProcessor.processListener(RabbitListenerAnnotationBeanPostProcessor.java:430) ~[spring-rabbit-2.2.1.RELEASE.jar:2.2.1.RELEASE]
at org.springframework.amqp.rabbit.annotation.RabbitListenerAnnotationBeanPostProcessor.processMultiMethodListeners(RabbitListenerAnnotationBeanPostProcessor.java:381) ~[spring-rabbit-2.2.1.RELEASE.jar:2.2.1.RELEASE]
at org.springframework.amqp.rabbit.annotation.RabbitListenerAnnotationBeanPostProcessor.postProcessAfterInitialization(RabbitListenerAnnotationBeanPostProcessor.java:299) ~[spring-rabbit-2.2.1.RELEASE.jar:2.2.1.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:431) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1807) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:595) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
... 15 common frames omitted
我的密码是
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import tp.application.SuperviseService;
import tp.core.event.DomainEventHandler;
import javax.annotation.Resource;
/**
* @author jh
* @date 2023/2/16 23:04
*/
@Component
@RabbitListener(bindings = @QueueBinding(
exchange = @Exchange(name = "trace_exchange"),
value = @Queue(value = "trace_queue")
))
public class CertificateApplyEventHandler implements DomainEventHandler<EnterpriseCertificateApplyEvent> {
private static final Logger LOGGER = LoggerFactory.getLogger(CertificateApplyEventHandler.class);
@Resource
SuperviseService superviseService;
@Override
@RabbitHandler
public void onApplicationEvent(EnterpriseCertificateApplyEvent event) {
superviseService.onCertificateApply(event);
}
}
当我调试兔子寄存器代码时,我发现注释@Queue 和@Exchange 不起作用。 rabitListener bean 的字符串是:
@org.springframework.amqp.rabbit.annotation.RabbitListener(ackMode=, admin=, autoStartup=, bindings=[@org.springframework.amqp.rabbit.annotation.QueueBinding(admins=[], arguments=[], 声明=true, [email protected].Exchange(admins=[], arguments=[], autoDelete=false, declare=true, delayed=false, durable=true, ignoreDeclarationExceptions=false, 内部=false, name=, type=direct, value=), ignoreDeclarationExceptions=false, key=[], [email protected].Queue(admins=[], arguments=[], autoDelete=, declare=true, durable=, exclusive=, ignoreDeclarationExceptions=false, name=EnterpriseCertificateApplyEvent, value=EnterpriseCertificateApplyEvent))], 并发=, containerFactory=, errorHandler=, exclusive=false, executor=, group=, id= , priority=, queues=[], queuesToDeclare=[], returnExceptions=)
为什么我设置的名称和值不见了?