`

BeanCreationException: No unique bean of type

 
阅读更多
我定义了一个基类接口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,如:

Java代码 复制代码 收藏代码
  1. @Autowired  
  2.  public void setADao(@Qualifier("aDaoImpl") ADao adao) {   
  3.   this.adao= adao;   
  4.  }  
@Autowired
 public void setADao(@Qualifier("aDaoImpl") ADao adao) {
  this.adao= adao;
 }


另外一种方案是使用@Resource这个注解,其功能与@autowired差不多,但是可以通过指定bean name或bean type注入相关bean,默认是按name注入,比autowired灵活很多,如:
Java代码 复制代码 收藏代码
  1. @Resource  
  2. private ADao aDaoImpl;  
@Resource
private ADao aDaoImpl;



分享到:
评论

相关推荐

    org.springframework.web.servlet-3.0.1.RELEASE-A.jar

    nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userController' defined in ServletContext resource [/WEB-INF/springMVC-servlet.xml]: Error ...

    springAOP demo 带错误解决文档

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.config.internalAutoProxyCreator': Instantiation of bean failed; nested exception is org....

    关于springboot我出过的那些错误.docx

    错误五:org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping' 错误原因:...

    asm5.0.jar

    Nested in org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in URL [file:/E:/cloudwave-core/src/main/webapp/WEB- INF/classes/...

    spring框架进行简单的增加操作出现的错误,解决方法

    Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class ...

    com.springsource.org.objectweb.asm-3.2.0.jar

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customAreaService' defined in class path resource [applicationContext.xml]: Initialization of bean failed;...

    Maven解决jar冲突调试步骤

    nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myRealm' defined in ServletContext resource [/WEB-INF/spring/tbm_web_shiro.xml]: ...

    spring boot exception handler.zip

    《Spring Boot异常处理器详解》 在Java开发领域,Spring Boot以其简洁、高效的特性深受开发者喜爱。在实际项目中,处理异常是必不可少的一部分,Spring Boot为此提供了强大的异常处理机制。本篇将深入探讨Spring ...

    org.springframework.transaction-3.1.2.RELEASE.zip

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.transaction.interceptor.TransactionInterceptor#0': Error setting property values; nested ...

    解决SpringDataJPA报错:org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null w

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/...

    ssmDemo1:SSM框架整合练习项目 来自《2017-7黑马49期web》

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in file [F:\IdeaProjects\ssmDemo1\out\artifacts\ssmDemo1_war_exp

    hibernate错误解决方案

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Initialization of bean ...

    SSH整合项目中容易出现的错误

    Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener org.springframework.beans.factory.BeanCreationException: Error creating...

    java工程错误信息解析和总结

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in resource loaded through InputStream: Initialization of bean failed; nested exception is ...

    基于springboot+mybatis实现的外卖订餐系统源码+项目说明(毕设).zip

    nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [org/mybatis/spring/boot/autoconfigure...

    S2SH整合报错

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Instantiation of bean failed;...

    Spring Cloud Feign报错问题解决

    然而,在启动过程中却出现了org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource的错误信息,导致应用程序...

    java异常解决方案.docx

    * org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'biz' defined in class path... + 解决方案:检查 Spring 的 Bean 定义,是否存在语法错误或格式不正确的问题,尝试...

    spring知识总结

    另外,文档中提及了Spring框架在应用中可能遇到的一个错误提示:“BeanCreationException: Error creating bean with name 'sessionFactory'”。这通常是因为bean的配置或Spring与Hibernate配置冲突导致的。解决此类...

Global site tag (gtag.js) - Google Analytics