最近学了IDEA和SpringBoot+MyBatis了,正所谓学以致用,于是用所学的来做项目,单元测试时报了下面的异常:

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'adminController': Unsatisfied dependency expressed through field 'adminService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'adminService': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'adminMapper' defined in file [E:\IDEA Project\shfw\target\classes\com\kvc\shfw\admin\mapper\AdminMapper.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [org/mybatis/spring/boot/autoconfigure/MybatisAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate

检查了一遍又一遍,特别是application.properties关于mapper.xml的配置,启动类是否添加Mapper的注解,serviceImpl是否引入了mapper的接口,发现都没问题,于是上网搜索,试了很多种办法,方法大抵也是说要去检查上面的那几项,可是我都检查了N回了,又按照网友给的方案,在pom.xml的<build></build>中加入下面的代码:

 <!-- 添加资源 -->
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <!-- src/main/resources下的指定资源放行 -->
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.yml</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>


结果依然无效,一筹莫展之际,只有把目光转向控制台,从控制台打印的信息入手,此时,下面的代码引起我的注意:

An attempt was made to call a method that does not exist. The attempt was made from the following location:

    org.mybatis.spring.SqlSessionFactoryBean.buildSqlSessionFactory(SqlSessionFactoryBean.java:426)

The following method did not exist:

    org.apache.ibatis.session.Configuration.setVfsImpl(Ljava/lang/Class;)V

The method's class, org.apache.ibatis.session.Configuration, is available from the following locations:

    jar:file:/C:/Users/shensh.CREATIVE/.m2/repository/org/mybatis/mybatis/3.2.8/mybatis-3.2.8.jar!/org/apache/ibatis/session/Configuration.class

It was loaded from the following location:

    file:/C:/Users/shensh.CREATIVE/.m2/repository/org/mybatis/mybatis/3.2.8/mybatis-3.2.8.jar


Action:

Correct the classpath of your application so that it contains a single, compatible version of org.apache.ibatis.session.Configuration

就是最后一行代码:Correct the classpath of your application so that it contains a single, compatible version of org.apache.ibatis.session.Configuration,把该行代码复制去搜索,有人说是jar冲突,而我的异常有提到mybatis-3.2.8.jar,会不会是mybatis jar冲突了呢,于是打开pom.xml,搜索mybatis,果不其然,有两个地方引入了关于mybatis中,一处是如下:

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>${mybatis.spring.boot.starter.version}</version>
</dependency>

另一处是:

<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.2.8</version>
</dependency>

我把后面这个引入去掉,重新启动,终于正常了。

这个问题给我体会是:

1.同样的异常,产生的原因有千奇百样种,不对着根源(控制台打印信息)去找解决方案,人云亦云,用别人的办法解决你的异常,由于异常产生的根源不同,那么这时问题解决不了的。我经常这样,异常产生了,一大堆英文,懒得去看,去翻译,直接复制到网上找解决办法,这点很不好,以后要痛改前非,遇到异常不慌张,不着急,静下心来好好理解下控制台的意思,再有针对性地寻求解决方案,远比遇到异常就像个无头苍蝇似的,到处乱撞,生搬硬套别人的解决办法,要强得多,要更快找到适合的解决办法。

2.mybatis-spring-boot-starter jar已经集成了mybatis的jar包了,不必要单独引入mybatis的jar了。

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐