org.springframework.context.annotation.ConflictingBeanDefinitionException:bean定义冲突

闲来无事,复习下spring,看了下尚硅谷spring相关的资料,导入示例代码,启动的时候,发现报了一个错误,如下:

Caused by: org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'userDao' for bean class [com.atguigu.spring.annotation.UserDao] conflicts with existing, non-compatible bean definition of same name and class [com.atguigu.spring.annotation.generic.UserDao]
	at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.checkCandidate(ClassPathBeanDefinitionScanner.java:314)
	at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.doScan(ClassPathBeanDefinitionScanner.java:253)
	at org.springframework.context.annotation.ComponentScanBeanDefinitionParser.parse(ComponentScanBeanDefinitionParser.java:85)
	at org.springframework.beans.factory.xml.NamespaceHandlerSupport.parse(NamespaceHandlerSupport.java:74)
	at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1424)
	at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1414)
	at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:187)
	at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.doRegisterBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:141)
	at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:110)
	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:508)
	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:391)
	... 14 more

ConflictingBeanDefinitionException:翻译过来【bean定义冲突异常】,应该是那UserDao这个bean注入到spring容器中冲突了,存在同名的bean,查看了下代码果然是:

org.springframework.context.annotation.ConflictingBeanDefinitionException:bean定义冲突_第1张图片

存在两个相同的bean【UserDao】,虽然在不同的包下面,但是配置文件里面扫描的包是com.atguigu.spring.annotation,下面的UserDao所在的包属于com.atguigu.spring.annotation这个包的子包,自然可以扫描到,这样spring容器中就存在两个类名相同的bean了,就冲突了,去掉下面得UserDao中的@Component注解,不让其注入到spring容器中,这样就不会冲突了;

你可能感兴趣的:(Spring)