SpringBootAnnotation-specified bean name ‘XXXXClass‘ for bean class [com.xx.XXXXClass] conflicts

Posted 幽灵雾

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBootAnnotation-specified bean name ‘XXXXClass‘ for bean class [com.xx.XXXXClass] conflicts相关的知识,希望对你有一定的参考价值。

【问题现象】

在SpringBoot启动运行的过程中,会报如下错误,如图:

Annotation-specified bean name 'XXXXClass' for bean class [com.xx.XXXXClass] conflicts with existing, non-compatible bean definition of same name and class [com.xxx.com.xx.XXXXClass]

org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.xx.XXXXClass] ; nested exception is org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'XXXXClass' for bean class [com.xxx.com.xx.XXXXClass]conflicts with existing, non-compatible bean definition of same name and class [com.xx.XXXXClass] 
	at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:189) ~[spring-context-5.3.22.jar:5.3.22]
	at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:331) ~[spring-context-5.3.22.jar:5.3.22]
	at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:247) ~[spring-context-5.3.22.jar:5.3.22]
	at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:311) ~[spring-context-5.3.22.jar:5.3.22]
	at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:112) ~[spring-context-5.3.22.jar:5.3.22]
	at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:746) ~[spring-context-5.3.22.jar:5.3.22]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:564) ~[spring-context-5.3.22.jar:5.3.22]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:147) ~[spring-boot-2.7.2.jar:2.7.2]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:734) [spring-boot-2.7.2.jar:2.7.2]
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408) [spring-boot-2.7.2.jar:2.7.2]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:308) [spring-boot-2.7.2.jar:2.7.2]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306) [spring-boot-2.7.2.jar:2.7.2]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1295) [spring-boot-2.7.2.jar:2.7.2]
	at com.kid.topten.ToptenApplication.main(ToptenApplication.java:10) [classes/:na]

【原因】

非常清晰,这个错误是说有两个重名的Bean都需要注入,所以会产生冲突。
可以通过提示,或者通过文件查找,找到具体重名的Bean:
[com.xxx.com.xx.XXXXClass]、 [com.xx.XXXXClass]

【解决】

最容易的解决办法,就是把其中一个名做修改,只需要名称不同即可。

特别的,如果和引用类库中的Bean重名,那就只能修改你自定义的类名了。

以上是关于SpringBootAnnotation-specified bean name ‘XXXXClass‘ for bean class [com.xx.XXXXClass] conflicts的主要内容,如果未能解决你的问题,请参考以下文章