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系统吗?不是把。。。

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

你可能感兴趣的:(expect)