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. iOS多线程编程

    废话不多说,直接上干货.先熟悉一下基本知识,然后讲一下常用的两种,NSOperation和GCD. 一.基础概念 进程: 狭义定义:进程是正在运行的程序的实例(an instance of a com ...

  2. Configuration Extensions - 简化配置,让你配置支持变量

    在开发"RabbitCloud"项目时,使用配置文件发现会有很多重复值,所以我基于"Microsoft.Extensions.Configuration"写了一 ...

  3. Routing in ASP.NET Core

    .NET-Core Series Server in ASP.NET-Core DI in ASP.NET-Core Routing in ASP.NET-Core Error Handling in ...

  4. mybatis入门篇基——基本配置与参数说明

    Mybatis 好吧这是我第一次写这种文章~如果有不足和错误之处欢迎评论,指点.今天想谈谈关于mybatis的一些基础入门知识. 进入正题~~: a.关于mybatis: 我个人觉得mybatis深得 ...

  5. 【京东账户】——Mysql/PHP/Ajax爬坑之页头页尾加载

    一.引言 实现京东的账户项目,有一个小功能,页头页尾加载.要用到的是Apach环境,Mysql.PHP以及Ajax. 二.实现 原理: 用php文件分别写一个的页头和一个页尾,放在前后两个div里. ...

  6. 协议端口号(protocol port number)

    协议端口号(protocol port number) 先来个注意事项 (-> ->) 这种在协议层间的抽象的协议端口是软件端口,和硬件端口是完全不同的概念.硬件端口是不同设备进行交互的接 ...

  7. java基础——java.util.ConcurrentModificationException

    在编写代码的时候,有时候会遇到List里有符合条件的的对象,就移除改对象! 但是这种操作如:使用了 List 的remove,会导致一些很严重的问题! 如下这段代码使用ArrayList: @Test ...

  8. bzoj1051(明星奶牛)

    这道就是明星奶牛,A了一次又一次了,(⊙o⊙)-(⊙o⊙)- 去年pas就打了不下5次,就是强联通缩点,然后求出度为0的块 判断有多个的话就无解,一个就输出块的大小. #include<cstd ...

  9. 如何用java语言获取某个网页的源代码

    import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URL; public class W ...

  10. Day3 Python基础学习——文件操作、函数

    一.文件操作 1.对文件操作流程 打开文件,得到文件句柄并赋值给一个变量 通过文件句柄对文件进行操作 关闭文件 #打开文件,读写文件,关闭文件 http://www.cnblogs.com/linha ...