Bug:关于expected single matching bean but found 2的问题

关于expected single matching bean but found 2的问题

问题:

org.springframework.context.support.AbstractApplicationContext refresh
警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userLoginServlet' defined in class path resource [spring.xml]: Unsatisfied dependency expressed through bean property 'userDao'; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'cn.tedu.spring.UserDao' available: expected single matching bean but found 2: userDao,subUserDao
Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userLoginServlet' defined in class path resource [spring.xml]: Unsatisfied dependency expressed through bean property 'userDao'; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'cn.tedu.spring.UserDao' available: expected single matching bean but found 2: userDao,subUserDao
	
***——>Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'cn.tedu.spring.UserDao' available: expected single matching bean but found 2: userDao,subUserDao

分析:使用了根据类型(byType)的自动装配方式,这种自动装配要求最多只有1个匹配类型的对象,但是,在Spring容器中却出现了2个这样的对象,Spring就不知道应该装配哪一个了,就会出现错误!

解决方案:使得Spring只管理其中1个类型的对象,或者,使用byName方式来装配。

异常:NoUniqueBeanDefinitionException:不唯一的Bean匹配异常

posted @   Charles博客  阅读(1912)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· NetPad:一个.NET开源、跨平台的C#编辑器
· PowerShell开发游戏 · 打蜜蜂
· 凌晨三点救火实录:Java内存泄漏的七个神坑,你至少踩过三个!
点击右上角即可分享
微信分享提示