The bean ‘queue01‘, defined in class path resource [com/haut/edu/seckill/config/RabbitMQTopicConfig.

问题

运行SpringBoot项目,控制台报错

The bean 'queue01', defined in class path resource [com/haut/edu/seckill/config/RabbitMQTopicConfig.class], could not be registered. A bean with that name has already been defined in class path resource [com/haut/edu/seckill/config/RabbitMQDirectConfig.class] and overriding is disabled.

详细问题

运行SpringBoot项目,控制台报错

2023-06-18 15:44:31.487  WARN 22832 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.support.BeanDefinitionOverrideException: Invalid bean definition with name 'queue01' defined in class path resource [com/haut/edu/seckill/config/RabbitMQTopicConfig.class]: Cannot register bean definition [Root bean: class [null]; scope=; abstract=false; lazyInit=null; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=rabbitMQTopicConfig; factoryMethodName=queue01; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [com/haut/edu/seckill/config/RabbitMQTopicConfig.class]] for bean 'queue01': There is already [Root bean: class [null]; scope=; abstract=false; lazyInit=null; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=rabbitMQDirectConfig; factoryMethodName=queue01; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [com/haut/edu/seckill/config/RabbitMQDirectConfig.class]] bound.
2023-06-18 15:44:31.494  INFO 22832 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2023-06-18 15:44:31.496 ERROR 22832 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

The bean 'queue01', defined in class path resource [com/haut/edu/seckill/config/RabbitMQTopicConfig.class], could not be registered. A bean with that name has already been defined in class path resource [com/haut/edu/seckill/config/RabbitMQDirectConfig.class] and overriding is disabled.

Action:

Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true


Process finished with exit code 1

解决方案一

若报错中所提及的方法实际运行时只有一个需要调用
删除报错中所提及的com/haut/edu/seckill/config/RabbitMQTopicConfig.class文件中queue01方法的Bean注解或删除报错中所提及的com/haut/edu/seckill/config/RabbitMQDirectConfig.class文件中queue01方法的Bean注解

解决方案二

若报错中所提及的方法实际运行时都需要调用
修改报错中所提及的com/haut/edu/seckill/config/RabbitMQTopicConfig.class文件中queue01方法的方法名或修改报错中所提及的com/haut/edu/seckill/config/RabbitMQDirectConfig.class文件中queue01方法的Bean方法名

解决方案三

在SpringBoot配置文件中增加配置如下配置
若使用application.properties作为配置文件

spring.main.allow-bean-definition-overriding=true

若使用application.yml作为配置文件

spring:
  main:
    allow-bean-definition-overriding: true

解决原因

问题的报错信息指出,在注册 bean 名称为 ‘queue01’ 的 bean 时出现了冲突。冲突的原因是,‘queue01’ 这个名称的 bean 在两个类中都被定义了,分别是 com.haut.edu.seckill.config.RabbitMQTopicConfig 和 com.haut.edu.seckill.config.RabbitMQDirectConfig。由于禁用了覆盖(overriding),因此无法将同一个名称的 bean 注册两次。
对于SpringBoot 项目中的Bean实例注入,需保证注入的Bean实例对象可以被SpringBoot唯一识别
笔者由于两个函数名称一致,参数类型一致,参数个数一致,SpringBoot无法找到唯一符合要求的Bean实例注入至项目中,上述三种解决方案,无论时方案一的删除,或是方案二的修改,或是方案三在SpringBoot配置文件中增加配置,表示允许项目(spring)的主函数(main)运行时,允许对于重复定义的Bean实例对象进行覆盖(allow-bean-definition-overriding=true),实际上都是保证注入的Bean实例对象可以被SpringBoot唯一识别。

原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
在这里插入图片描述

你可能感兴趣的:(spring)