No qualifying bean of type ‘com.oa.mapper.DeptMapper‘ available Error creating bean with name

目录:

  • 1.报错描述
    • 2.报错原因
    • 3.解决方法

1.报错描述

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ‘com.oa.mapper.DeptMapper’ available(最核心的报错): expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}


Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘deptServiceImpl’: Unsatisfied dependency expressed through field ‘baseMapper’; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ‘com.oa.mapper.DeptMapper’ available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}


Error creating bean with name ‘deptController’: Unsatisfied dependency expressed through field ‘deptService’; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'deptServiceImpl’: Unsatisfied dependency expressed through field ‘baseMapper’; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ‘com.oa.mapper.DeptMapper’ available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
[RMI TCP Connection(3)-127.0.0.1] ERROR org.springframework.web.context.ContextLoader - Context initialization failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘deptController’: Unsatisfied dependency expressed through field 'deptService’; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘deptServiceImpl’:

2.报错原因

  • 没有“com.oa.mapper.DeptMapper”类型的合格bean可用
  • mapper层中的XxxMapper接口文件没交给ioc容器管理(未产生对应该接口的bean),导致当其他配置需要该bean时,产生报错,从而引起了一系列的报错!

No qualifying bean of type ‘com.oa.mapper.DeptMapper‘ available Error creating bean with name_第1张图片

3.解决方法

将mapper层下的 XxxMapper接口文件交给ioc容器管理。
通过 以下配置(配置MapperScannerConfigurer) 会扫描com.oa.mapper下的所有接口,然后创建各自接口的 “动态代理类”。这样,就能成功注入bean对象了。
(扫描mapper接口,注册mapper接口,生成mapper接口的“动态代理类”)

http://www.springframework.org/schema/beans/spring-beans.xsd

ps:
放在spring.xml(也叫:applicationContext.xml)配置文件 的<beans ... >
     <!--
       把mapper层下的 XxxMapper接口文件交给ioc容器管理
      -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--  动态代理生成(反射)对象 -->
        <property name="basePackage" value="com.oa.mapper"/>
    </bean>
    
ps:
放在spring.xml(也叫:applicationContext.xml)配置文件 的<beans> ... </beans>

你可能感兴趣的:(spring,mybatis,java,后端)