今天在调试代码时,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