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的更多相关文章

  1. spring " expected single matching bean but found 2" 问题一例。

    初入java,使用spring时遇到一个问题,左边是一个接口和实现.右边是service和实现. @Service@Transactional(rollbackFor = Exception.clas ...

  2. spring依赖注入单元测试:expected single matching bean but found 2

    异常信息:org.springframework.beans.factory.UnsatisfiedDependencyException: Caused by: org.springframewor ...

  3. MyBatis Plus:No qualifying bean of type 'com.baomidou.mybatisplus.mapper.BaseMapper<?>' available: expected single matching bean but found 4

    场景: 应用MyBatis Plus 和通用Mapper 继承自ServiceImpl实现对Service里的方法进行包装再处理. public interface IServiceBase2< ...

  4. 深入Spring Boot:怎样排查expected single matching bean but found 2的异常

    写在前面 这个demo来说明怎么排查一个常见的spring expected single matching bean but found 2的异常. https://github.com/hengy ...

  5. 多数据源报错 expected single matching bean but found 2: xxx,xxx

    问题: expected single matching bean but found 2: xxx,xxx 原因:在 Spring 容器中配置了两个类型Bean,Spring 容器将无法确定到底要用 ...

  6. Spring 3.2 @Autowired异常:expected single matching bean but found 2

    在使用Sping做单元测试时候,对RequestMappingHandlerAdapter(从处理器包装过来的适配器)进行自动装配, 发现报:expected single matching bean ...

  7. 多个@bean无法通过@resource注入对应的bean(org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'javax.sql.DataSource' available: expected single matching bean but found )

    一.异常 org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type ' ...

  8. Spring Boot 自定义配置文件异常"expected single matching bean but found 2"

    运行环境:Spring Boot 2.5.0, IDEA 2020.3.2 异常详细信息: Injection of resource dependencies failed; nested exce ...

  9. expected single matching bean but found 2: menusServiceImpl,IMenusService

    问题如下: 接口也作为匹配的bean? 有点迷惑了....... 经过在网上找资料,发现和@MapperScan这个注解有关系,具体源码不止.但是这个注解会扫描路径下的所有类. 去掉这个注解就可以正常 ...

随机推荐

  1. Oracle日期时间操作大全

    本文出自:http://www.cnblogs.com/hl3292/archive/2010/11/03/1868159.html oracle sql日期比较: 共三部分: 第一部分:oracle ...

  2. IIS部署新网站

    Windows Server使用IIS 6.0配置ASP动态Web网站 http://jingyan.baidu.com/article/c1a3101ee43ae9de656debb4.html h ...

  3. ambari

    http://blog.csdn.net/ggz631047367/article/details/50491616 https://cwiki.apache.org/confluence/displ ...

  4. Tomcat代码执行漏洞(CVE-2017-12615)的演绎及个人bypass

    0x00 漏洞简介 2017年9月19日,Apache Tomcat官方确认并修复了两个高危漏洞. 漏洞CVE编号:CVE-2017-12615和CVE-2017-12616. 其中 远程代码执行漏洞 ...

  5. CSS1-3基礎知識

    CSS1-3基礎知識 1.css排版 css在html內排版: <style type='text/css'> 標記名{} .類型名{} #ID名{} 標記名,.類型名,#ID名{} &l ...

  6. poj1006中国剩余定理

    Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 103506   Accepted: 31995 Des ...

  7. Prison Break

    Prison Break 时间限制: 1 Sec  内存限制: 128 MB提交: 105  解决: 16[提交][状态][讨论版] 题目描述 Scofild又要策划一次越狱行动,和上次一样,他已经掌 ...

  8. 玩转 sublime3 第二弹 ES6环境

    安装node: node作为JS的运行环境必须安装 文件下载:https://nodejs.org/dist/v6.11.4/node-v6.11.4-x64.msi 备注:可以去官网 https:/ ...

  9. 用python爬虫爬取去哪儿4500个热门景点,看看国庆不能去哪儿

    前言:本文建议有一定Python基础和前端(html,js)基础的盆友阅读. 金秋九月,丹桂飘香,在这秋高气爽,阳光灿烂的收获季节里,我们送走了一个个暑假余额耗尽哭着走向校园的孩籽们,又即将迎来一年一 ...

  10. SpringMVC加载.roperties文件属性值的方法?

    1.在xml文件中引入来获取属性值就不说了. 2.在controller层获取引用配置文件中的属性值: (1).编写工具类 @Configuration @PropertySource(value=& ...