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. java: Multiple encodings set for module chunk test "GBK" will be used by compiler

    IDEA 进行编译代码的时候,特别是新项目 特别容易出现 编码错误,但是 File-Encoding中设置的又没有问题,而且maven 是能打包的,就是用 idea 自带的 编译的时候 就会出现提示 ...

  2. StringBuffer和String的相互转换

    1:用法: * A:String -- >StringBuffer * a:通过构造方法 * b:通过append()方法 * B:StringBuffer --> String * a: ...

  3. 参加Java培训你必须知道的五点真相!

    相信大家都有过到招聘网站投简历.找工作的经历.当一份份简历发出三天后,左等右等连一个电话没有等来,心中不免有些失落,有些焦虑.这个时侯,很多培训机构就会纷纷给你打电话以各种名义让你参加各种IT技能培训 ...

  4. 第一个asp.net MVC5+ExtJS6入门案例项目

    最近在学习asp.net MVC,结合前段时间学习的ExtJS,做了一个入门示例.不过还有一个json日期显示的问题没有解决. [思路] 1.先搭建一个asp.net MVC项目. 2.将MVC项目的 ...

  5. M方法

    ThinkPHP函数详解:M方法 M方法用于实例化一个基础模型类,和D方法的区别在于:1.不需要自定义模型类,减少IO加载,性能较好:2.实例化后只能调用基础模型类(默认是Model类)中的方法:3. ...

  6. 课程作业02(关于Java的几点讨论)

    ---恢复内容开始--- 1.一个Java类文件中真的只能有一个公有类吗? public class Test { public static void main(String[] args) { } ...

  7. fitnesse - Variables and Symbols

    fitnesse - Variables and Symbols 2017-09-30 目录 1 Variables(静态变量)  1.1 定义及使用  1.2 Variable作用域    1.2. ...

  8. 初学者易上手的SSH-struts2 01环境搭建

    首先,SSH不是一个框架,而是多个框架(struts+spring+hibernate)的集成,是目前较流行的一种Web应用程序开源集成框架,用于构建灵活.易于扩展的多层Web应用程序. 集成SSH框 ...

  9. Message Queuing(MSMQ)

    一.前言 MicroSoft Message Queuing(微软消息队列)是在多个不同的应用之间实现相互通信的一种异步传输模式,相互通信的应用可以分布于同一台机器上,也可以分布于相连的网络空间中的任 ...

  10. samba的安装和配置

    samba是Linux系统上的一种文件共享协议,可以实现Windows系统访问Linux系统上的共享资源,现在介绍一下如何在Ubuntu 14.04上安装和配置samba 实验环境 Ubuntu 14 ...