本文主要是介绍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异常问题的解决的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!