BeanCreationException: No unique bean of type
来源:互联网 发布:织梦 主页 编辑:程序博客网 时间:2024/06/07 03:34
我定义了一个基类接口BaseDao,下面有些update\save的方法;
然后我用一个BaseDaoImpl去实现这个接口;好啦,然后我现在有两个Dao接口,一个ADao extends BaseDao,一个BDao extends BaseDao; 然后再有这两个Dao的实现: ADaoImpl extends BaseDaoImpl implements ADao; BDaoImpl extends BaseDaoImpl implements BDao;
这两个实现都加了@repository。结果就是启动错:
No unique bean of type [com.a.b.BaseDao] is defined: expected single matching bean but found 2: [aDaoImpl, bDaoImpl]
出现这个异常的原因是因为我用了@Autowird这个注解,这个注解是根据类型的方式搜索匹配的,找到了两个相符的依赖类,对于上述配置就是找到了basedao 的两个 bean: adaoimpl , bdaoimpl。
对于这种同类型class有多个实例的解决方案的一种方案是继续延用autowired,不过通过@Qualifier指明是哪个名字的bean,如:
另外一种方案是使用@Resource这个注解,其功能与@autowired差不多,但是可以通过指定bean name或bean type注入相关bean,默认是按name注入,比autowired灵活很多,如:
然后我用一个BaseDaoImpl去实现这个接口;好啦,然后我现在有两个Dao接口,一个ADao extends BaseDao,一个BDao extends BaseDao; 然后再有这两个Dao的实现: ADaoImpl extends BaseDaoImpl implements ADao; BDaoImpl extends BaseDaoImpl implements BDao;
这两个实现都加了@repository。结果就是启动错:
No unique bean of type [com.a.b.BaseDao] is defined: expected single matching bean but found 2: [aDaoImpl, bDaoImpl]
出现这个异常的原因是因为我用了@Autowird这个注解,这个注解是根据类型的方式搜索匹配的,找到了两个相符的依赖类,对于上述配置就是找到了basedao 的两个 bean: adaoimpl , bdaoimpl。
对于这种同类型class有多个实例的解决方案的一种方案是继续延用autowired,不过通过@Qualifier指明是哪个名字的bean,如:
Java代码
- @Autowired
- public void setADao(@Qualifier("aDaoImpl") ADao adao) {
- this.adao= adao;
- }
另外一种方案是使用@Resource这个注解,其功能与@autowired差不多,但是可以通过指定bean name或bean type注入相关bean,默认是按name注入,比autowired灵活很多,如:
Java代码
- @Resource
- private ADao aDaoImpl;
原文地址:http://hi.baidu.com/laxinicer/blog/item/7dba4635aaf2ea94a61e12c9.html
- BeanCreationException: No unique bean of type
- BeanCreationException: No unique bean of type
- No unique bean of type
- No unique bean of type
- spring exception--No unique bean of type
- Spring注解No unique bean of type
- Spring JavaConfig - NoSuchBeanDefinitionException: No unique bean of type
- No unique bean of type [ *.service.*Service] is defined
- No unique bean of type [javax.persistence.EntityManagerFactory] is defined
- No unique bean of type is defined: expected single bean but found 0
- No CacheResolver specified, and no unique bean of type CacheManager found
- No qualifying bean of type
- Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [
- paip . 解决spring No unique bean of type [com.mijie.homi.search.service.index.MoodUserIndexService]
- spring 依赖注入用@Autowired报错 No unique bean of type
- No unique bean of type [net.shougongfang.action.paymoney.AlipayPayMoneyReturnObj] is defined: Unsat
- No unique bean of type [com.yuanv.backstage.dao.SysUserDao] is defined: expected single matching bea
- org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.yuanv.b
- MFC简介
- 对n个不同字母的全排列
- hdu 2243 考研路茫茫——单词情结
- 常见设计模式的解析和实现(C++)之十-Proxy模式
- OpenGL像素格式-PIXELFORMATDESCRIPTOR
- BeanCreationException: No unique bean of type
- JSON for java入门总结
- linux 系统中的 7个级别
- 游戏开发学习资料
- [数据结构]C++队列实现和队列简介
- PyDev for Eclipse 简介 和各种功能设置
- c#(winform)只输入数字、退格、小数点
- Nand Flash结构与读写
- 一个DC拷贝到内存DC上。