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

question

Running the SpringBoot project, the console reports an error

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.

detailed question

Running the SpringBoot project, the console reports an error

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

solution one

If only one of the methods mentioned in the error report actually runs, you need to call to
delete the Bean annotation of the queue01 method in the com/haut/edu/seckill/config/RabbitMQTopicConfig.class file mentioned in the error report or delete the one mentioned in the error report Bean annotation of queue01 method in com/haut/edu/seckill/config/RabbitMQDirectConfig.class file

solution two

If the method mentioned in the error report actually runs, you need to call and
modify the method name of the queue01 method in the com/haut/edu/seckill/config/RabbitMQTopicConfig.class file mentioned in the error report or modify the com mentioned in the error report The Bean method name of the queue01 method in the /haut/edu/seckill/config/RabbitMQDirectConfig.class file

Solution three

Add the following configuration in the SpringBoot configuration file
If you use application.properties as the configuration file

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

If you use application.yml as the configuration file

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

solve the cause

The error message for the problem states that there was a conflict in registering a bean with bean name 'queue01'. The reason for the conflict is that the bean with the name 'queue01' is defined in two classes, com.haut.edu.seckill.config.RabbitMQTopicConfig and com.haut.edu.seckill.config.RabbitMQDirectConfig. Since overriding is disabled, it is not possible to register a bean with the same name twice.
For the Bean instance injection in the SpringBoot project, it is necessary to ensure that the injected Bean instance object can be uniquely identified by SpringBoot.
Because the two function names are the same, the parameter types are the same, and the number of parameters is the same, SpringBoot cannot find the only Bean instance that meets the requirements and inject it into In the project, the above three solutions, regardless of the deletion of the first solution, the modification of the second solution, or the addition of the configuration in the SpringBoot configuration file of the third solution, means that when the main function (main) of the project (spring) is allowed to run, Allowing the overriding of repeatedly defined Bean instance objects (allow-bean-definition-overriding=true) actually ensures that the injected Bean instance objects can be uniquely identified by SpringBoot.

It’s not easy
to be original, please indicate the source
if it’s helpful to you, don’t forget to like it and support it
insert image description here

Guess you like

Origin blog.csdn.net/T_Y_F_/article/details/131277511
Recommended