Spring+MyBatis整合开发时 Injection of autowired dependencies failed错误的解决

在Spring+MyBatis整合开发过程中遇到'Injection of autowired dependencies failed'错误,实际上是由于MyBatis映射文件中resultType和parameterType的数据类型错误引起。通过Code Review和调试找到问题根源,纠正了MyBatis映射文件的错误。在排查问题时,不应忽视MyBatis配置,而应系统地检查所有可能的错误源。提供了解决此类问题的步骤和注意事项。
摘要由CSDN通过智能技术生成

今天在调试代码时,Spring启动后出现了错误:Injection of autowired dependencies failed;……的错误。

详细的错误信息如下:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userServiceImpl': Injection of autowired dependencies failed;

经过一个多小时的Code Review和调试终于解决了问题,最后总结解决问题的方法,其实是MyBatis的一个映射文件中的resultType=”java.lang.Integer” parameterType=”java.lang.Integer”写错误。

但是这个错误却引发了Injection of autowired dependencies failed;……的错误。一开始以为是Spring配置文件错误了;再后来仔细检查各个包下面的注解;最后检查MyBatis配置文件时才发现了错误。

浪费时间的地方在于:一开始总是认为Spring配置文件和Spring注解使用错误;没有仔细检查MyBatis的映射文件。

在解决这个问题的过程中,在网上发现其实很多帖子,很多开发者都遇到过类似的问题;但是发现很多帖子都没有给出完整、准确的解决方法。所以总结一下解决过程和思路。

样例代码如下:

1. Spring配置文件applicationContext.xml的要点

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

    <!-- 向 Spring 容器注册 AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、 
        PersistenceAnnotationBeanPostProcessor 以及 RequiredAnnotationBeanPostProcessor 
        这 4 个BeanPostProcessor。 注册这4个 BeanPostProcessor的作用,就是为了你的系统能够识别相应的注解。 -->
    <context:annotation-config />

    <!-- 在xml配置了这个标签后,spring可以自动去扫描base-pack下面或者子包下面的Java文件,如果扫描到有 @Component 
        @Controller@Service等这些注解的类,则把这些类注册为bean. 注意:如果配置了<context:component-scan> 
        那么<context:annotation-config/>标签就可以不用再xml中配置了,因为前者包含了后者。 -->
    <context:component-scan base-package="com.keymen.*" />

    <bean
        class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.keymen.dao" />
        <property name="annotationClass" value="com.keymen.annotation.MyBatisDAO" />
    </bean>
    <!-- 在spring3以上版本中使用spring的依赖注入(注解或者xml方式)和aop功能时, 如果不设置<aop:aspectj-autoprox
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值