首页 > 其他 > 详细

expected single matching bean but found 2

时间:2017-11-01 16:21:52      阅读:1588      评论:0      收藏:0      [点我收藏+]
1 org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘accountAction‘: Unsatisfied dependency expressed through field ‘accountService‘; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type ‘com.xxx.IAccountService‘ available: expected single matching bean but found 2: accountServiceImpl,IAccountService

让我很是疑惑,为什么会产生这个错误。

查资料很多说是一个接口有两个实现类,在引用的时候单纯的使用Autowire就会出现上述错误。但是我看了一下项目中的代码,没有出现这种情况。

尝试过很多次后,找到问题了,是因为MyBatis的MapperScannerConfigurer的配置引起的。

项目中出了MapperScannerConfigurer的包名扫描配置以外,还有一处Spring的配置, <context:component-scan base-package="com.xxx.yyy" /> 

MyBatis的MapperScannerConfigurer 的配置:

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
   <property name="basePackage" value="com.xxx.yyy" />
   <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>

修改为:

<context:component-scan base-package="com.xxx.yyy" />

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
   <property name="basePackage" value="com.xxx.yyy.**.dao" />
   <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>

解决问题。

跟了一下MapperScannerConfigurer 源代码,MapperScannerConfigurer 会把 配置的basePackage下面的mapper接口扫描到,并将他们注册到spring容器中去。


如果配置的路径范围过大,spring base-package 和 mybatis basePackage 下面的bean会有重复注册的现象,就会出现文章开头的错误了。

expected single matching bean but found 2

原文:http://www.cnblogs.com/goalone/p/7766863.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!