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. Nexus5/6刷 lineageos 过程

    如题,好久没刷机,体验下lineageos 由于之前Xposed官方一直没支持7.x,就没更新 测试三方Xposed正常使用,记录下过程供参考 注意,刷机有风险,刷机前备份,可以使用钛备份 0x01: ...

  2. CSV导出大量数据

    $csvname = $csvname . '.csv'; header('Content-Type: application/vnd.ms-excel;charset=GB2312'); heade ...

  3. Writing Science 笔记 6.20

    1.写作的六个要素:S: Simple 简单的 U: Unexpected 出人意料的 C: Concrete 具体的  C: Credible 可信的  E: Emotional S: Storie ...

  4. mongoDB学习手记1--Windows系统下的安装与启动

    第一步:下载安装包 我们首先需要下载 mongodb 的安装包,直接到官网下载即可.地址为:https://www.mongodb.com/download-center#community. 看下自 ...

  5. 【POJ】 1061 青蛙的约会(扩欧)

    青蛙的约会 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 119148   Accepted: 25070 Descript ...

  6. LCM Cardinality 暴力

    LCM Cardinality Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit St ...

  7. .Neter玩转Linux系列之二:Linux下的文件目录及文件目录的权限

    一.Linux下的文件目录 简介:linux的文件系统是采用级层式的树状目录结构,在此 结构中的最上层是根目录“/”,然后在此目录下再创建 其他的目录.深刻理解linux文件目录是非常重要的,如下图所 ...

  8. Docker入门之六端口映射与容器互联

    一.端口映射 在之前的博客搭建私有仓库时用到这样一句:docker run -d -p 5000:5000 -v /opt/data/registry:/tmp/registry registry来r ...

  9. asp.net 的发布与执行

    asp.net的工程文件在测试完成后就是发布(部署)和执行. 发布 本地发布的例子 1,菜单项  生成--发布 2,如果是第一次发布,点击自定义,设置配置文件的名称 3,发布方法--文件系统,设置本地 ...

  10. Python系列之模块、和字符串格式化

    Python 模块 模块让你能够有逻辑地组织你的Python代码段. 把相关的代码分配到一个 模块里能让你的代码更好用,更易懂. 模块也是Python对象,具有随机的名字属性用来绑定或引用. 模块分为 ...