`
周一Monday
  • 浏览: 342641 次
  • 来自: 北京
社区版块
存档分类
最新评论

No unique bean of type[....BaseDao] expected single matching bean but found..

 
阅读更多

异常信息:

 


Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'deptServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springfuncs.common.dao.BaseDao org.springfuncs.common.service.impl.BaseServiceImpl.dao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [org.springfuncs.common.dao.BaseDao] is defined: expected single matching bean but found 2: [deptDaoImpl, empDaoImpl]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at org.springfuncs.test.SpringTest.main(SpringTest.java:12)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springfuncs.common.dao.BaseDao org.springfuncs.common.service.impl.BaseServiceImpl.dao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [org.springfuncs.common.dao.BaseDao] is defined: expected single matching bean but found 2: [deptDaoImpl, empDaoImpl]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:506)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284)
... 13 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [org.springfuncs.common.dao.BaseDao] is defined: expected single matching bean but found 2: [deptDaoImpl, empDaoImpl]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:800)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478)
... 15 more

解决方案:

 

@Transactional
public abstract class BaseServiceImpl<T> implements BaseService<T> {

 

private BaseDao<T> dao;

 

@Autowired
public void setDao(BaseDao<T> dao) {
this.dao = dao;
}

 

。。。。。。。。。。

 

 

--------------------------------

 

@Service
public class EmpServiceImpl extends BaseServiceImpl<Emp> implements EmpService {

 

@Override
@Autowired
@Qualifier("empDaoImpl")
public void setDao(BaseDao<Emp> dao) {
super.setDao(dao);
}

}

-----------------------------------

 

@Service
public class DeptServiceImpl extends BaseServiceImpl<Dept> implements DeptService {

@Override
@Autowired
@Qualifier("deptDaoImpl")
public void setDao(BaseDao<Dept> dao) {
super.setDao(dao);
}

 

}

 

附:晕。。。编辑器不好用。因为我是Win7系统吗?不是把。。。

所以,暂时先将代码贴上去把。。不美观啊。。。

分享到:
评论
4 楼 WanYanSir 2013-05-17  
No matching bean of type [com.spring.service.SysuserService] found for dependency????这是为什么???
3 楼 ITvision 2013-05-05  
尝试使用
@Service
public class EmpServiceImpl extends BaseServiceImpl<Emp> implements EmpService {

private DeptDao deptDao;
@resource
public void setDao(BaseDao<Dept> dao) {
super.setDao(dao);
this.deptDao=(DeptDao)dao;
}
}
2 楼 周一Monday 2012-11-07  
cs_dn007 写道
注意:[org.springfuncs.common.dao.BaseDao] is defined: expected single matching bean but found 2: [deptDaoImpl, empDaoImpl]
============================================
@Service的自动注入,与配置文件bean的配置重复了,spring容器有多个该bean
要么去掉配置,要么修改注解




你确定?

可能有时候写BaseService并不是最佳实践。
1 楼 cs_dn007 2012-11-07  
注意:[org.springfuncs.common.dao.BaseDao] is defined: expected single matching bean but found 2: [deptDaoImpl, empDaoImpl]
============================================
@Service的自动注入,与配置文件bean的配置重复了,spring容器有多个该bean
要么去掉配置,要么修改注解

相关推荐

Global site tag (gtag.js) - Google Analytics