org.springframework.beans.factory.UnsatisfiedDependencyException异常问题的解决

本文主要是介绍org.springframework.beans.factory.UnsatisfiedDependencyException异常问题的解决,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

最近学了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;)VThe 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.classIt was loaded from the following location:file:/C:/Users/shensh.CREATIVE/.m2/repository/org/mybatis/mybatis/3.2.8/mybatis-3.2.8.jarAction: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了。

这篇关于org.springframework.beans.factory.UnsatisfiedDependencyException异常问题的解决的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/443018

相关文章

如何解决idea的Module:‘:app‘platform‘android-32‘not found.问题

《如何解决idea的Module:‘:app‘platform‘android-32‘notfound.问题》:本文主要介绍如何解决idea的Module:‘:app‘platform‘andr... 目录idea的Module:‘:app‘pwww.chinasem.cnlatform‘android-32

kali linux 无法登录root的问题及解决方法

《kalilinux无法登录root的问题及解决方法》:本文主要介绍kalilinux无法登录root的问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,... 目录kali linux 无法登录root1、问题描述1.1、本地登录root1.2、ssh远程登录root2、

SpringBoot应用中出现的Full GC问题的场景与解决

《SpringBoot应用中出现的FullGC问题的场景与解决》这篇文章主要为大家详细介绍了SpringBoot应用中出现的FullGC问题的场景与解决方法,文中的示例代码讲解详细,感兴趣的小伙伴可... 目录Full GC的原理与触发条件原理触发条件对Spring Boot应用的影响示例代码优化建议结论F

MySQL 中查询 VARCHAR 类型 JSON 数据的问题记录

《MySQL中查询VARCHAR类型JSON数据的问题记录》在数据库设计中,有时我们会将JSON数据存储在VARCHAR或TEXT类型字段中,本文将详细介绍如何在MySQL中有效查询存储为V... 目录一、问题背景二、mysql jsON 函数2.1 常用 JSON 函数三、查询示例3.1 基本查询3.2

Pyserial设置缓冲区大小失败的问题解决

《Pyserial设置缓冲区大小失败的问题解决》本文主要介绍了Pyserial设置缓冲区大小失败的问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录问题描述原因分析解决方案问题描述使用set_buffer_size()设置缓冲区大小后,buf

PyInstaller打包selenium-wire过程中常见问题和解决指南

《PyInstaller打包selenium-wire过程中常见问题和解决指南》常用的打包工具PyInstaller能将Python项目打包成单个可执行文件,但也会因为兼容性问题和路径管理而出现各种运... 目录前言1. 背景2. 可能遇到的问题概述3. PyInstaller 打包步骤及参数配置4. 依赖

resultMap如何处理复杂映射问题

《resultMap如何处理复杂映射问题》:本文主要介绍resultMap如何处理复杂映射问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录resultMap复杂映射问题Ⅰ 多对一查询:学生——老师Ⅱ 一对多查询:老师——学生总结resultMap复杂映射问题

解决SpringBoot启动报错:Failed to load property source from location 'classpath:/application.yml'

《解决SpringBoot启动报错:Failedtoloadpropertysourcefromlocationclasspath:/application.yml问题》这篇文章主要介绍... 目录在启动SpringBoot项目时报如下错误原因可能是1.yml中语法错误2.yml文件格式是GBK总结在启动S

java实现延迟/超时/定时问题

《java实现延迟/超时/定时问题》:本文主要介绍java实现延迟/超时/定时问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java实现延迟/超时/定时java 每间隔5秒执行一次,一共执行5次然后结束scheduleAtFixedRate 和 schedu

Java Optional避免空指针异常的实现

《JavaOptional避免空指针异常的实现》空指针异常一直是困扰开发者的常见问题之一,本文主要介绍了JavaOptional避免空指针异常的实现,帮助开发者编写更健壮、可读性更高的代码,减少因... 目录一、Optional 概述二、Optional 的创建三、Optional 的常用方法四、Optio