启动spring boot + mybatis plus服务报Cannot resolve reference to bean 'sqlSessionTemplate' while setting bean property 'sqlSessionTemplate'
今天遇到一个问题,花了大量时间才解决了。记录一下,希望能帮助遇到同样问题的同学,也以免以后自己在遇到类似的问题。
报错原因是因为使用的spring boot版本与mybatis plus版本不匹配,解决问题在下面。
如果你也是和我一样花了大量时间去排除代码中的注解、文件路径、配置文件后发现这些都没问题,那大概率是这mybatis plus版本的问题。
报错信息如下:
具体报错信息如下:
2024-10-14 16:47:55,643 | WARN | main | org.springframework.context.support.AbstractApplicationContext[632] | Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'adminHomePageController': Unsatisfied dependency expressed through field 'orderService': Error creating bean with name 'orderServiceImpl' defined in file [E:\kxkd supplier\scm\scm-service-order\target\classes\scm\kxkd\service\order\service\impl\OrderServiceImpl.class]: Unsatisfied dependency expressed through constructor parameter 1: Error creating bean with name 'orderGroupServiceImpl': Unsatisfied dependency expressed through field 'baseMapper': Error creating bean with name 'orderGroupMapper' defined in file [E:\kxkd supplier\scm\scm-service-order\target\classes\scm\kxkd\service\order\mapper\OrderGroupMapper.class]: Cannot resolve reference to bean 'sqlSessionTemplate' while setting bean property 'sqlSessionTemplate' 2024-10-14 16:47:55,644 | INFO | main | com.zaxxer.hikari.HikariDataSource[350] | scm-service-order - Shutdown initiated... 2024-10-14 16:48:00,478 | INFO | main | com.zaxxer.hikari.HikariDataSource[352] | scm-service-order - Shutdown completed. 2024-10-14 16:48:00,480 | WARN | main | org.springframework.context.support.AbstractApplicationContext[1133] | Exception thrown from ApplicationListener handling ContextClosedEvent org.springframework.beans.factory.BeanCreationNotAllowedException: Error creating bean with name 'applicationTaskExecutor': Singleton bean creation not allowed while singletons of this factory are in destruction (Do not request a bean from a BeanFactory in a destroy method implementation!) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:220) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:335) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:205) at org.springframework.context.event.AbstractApplicationEventMulticaster.retrieveApplicationListeners(AbstractApplicationEventMulticaster.java:265) at org.springframework.context.event.AbstractApplicationEventMulticaster.getApplicationListeners(AbstractApplicationEventMulticaster.java:222) at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:145) at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:451) at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:457) at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:384) at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:1130) at org.springframework.context.support.AbstractApplicationContext.close(AbstractApplicationContext.java:1093) at org.springframework.cloud.context.named.NamedContextFactory.destroy(NamedContextFactory.java:114) at org.springframework.beans.factory.support.DisposableBeanAdapter.destroy(DisposableBeanAdapter.java:211) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroyBean(DefaultSingletonBeanRegistry.java:587) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingleton(DefaultSingletonBeanRegistry.java:559) at org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingleton(DefaultListableBeanFactory.java:1202) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingletons(DefaultSingletonBeanRegistry.java:520) at org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingletons(DefaultListableBeanFactory.java:1195) at org.springframework.context.support.AbstractApplicationContext.destroyBeans(AbstractApplicationContext.java:1186) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:637) at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754) at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:456) at org.springframework.boot.SpringApplication.run(SpringApplication.java:335) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1363) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1352) at scm.kxkd.service.order.OrderServiceApplication.main(OrderServiceApplication.java:26) ... *************************** APPLICATION FAILED TO START *************************** Description: An attempt was made to call a method that does not exist. The attempt was made from the following location: com.baomidou.mybatisplus.core.MybatisMapperAnnotationBuilder.parse(MybatisMapperAnnotationBuilder.java:122) The following method did not exist: 'void org.apache.ibatis.session.Configuration.parsePendingMethods(boolean)' The calling method's class, com.baomidou.mybatisplus.core.MybatisMapperAnnotationBuilder, was loaded from the following location: jar:file:/D:/tool_zip/maven/repositories/com/baomidou/mybatis-plus-core/3.5.6/mybatis-plus-core-3.5.6.jar!/com/baomidou/mybatisplus/core/MybatisMapperAnnotationBuilder.class The called method's class, org.apache.ibatis.session.Configuration, is available from the following locations: jar:file:/D:/tool_zip/maven/repositories/org/mybatis/mybatis/3.5.14/mybatis-3.5.14.jar!/org/apache/ibatis/session/Configuration.class The called method's class hierarchy was loaded from the following locations: org.apache.ibatis.session.Configuration: file:/D:/tool_zip/maven/repositories/org/mybatis/mybatis/3.5.14/mybatis-3.5.14.jar Action: Correct the classpath of your application so that it contains compatible versions of the classes com.baomidou.mybatisplus.core.MybatisMapperAnnotationBuilder and org.apache.ibatis.session.Configuration 2024-10-14 16:48:01,508 | WARN | Thread-3 | com.alibaba.nacos.common.http.HttpClientBeanHolder[102] | [HttpClientBeanHolder] Start destroying common HttpClient 2024-10-14 16:48:01,508 | WARN | Thread-5 | com.alibaba.nacos.common.notify.NotifyCenter[134] | [NotifyCenter] Start destroying Publisher 2024-10-14 16:48:01,509 | WARN | Thread-5 | com.alibaba.nacos.common.notify.NotifyCenter[151] | [NotifyCenter] Destruction of the end 2024-10-14 16:48:01,509 | WARN | Thread-3 | com.alibaba.nacos.common.http.HttpClientBeanHolder[111] | [HttpClientBeanHolder] Destruction of the end Disconnected from the target VM, address: '127.0.0.1:54215', transport: 'socket' Process finished with exit code 1
解决方法:更换引入的mybatis plus版本
特别是使用springboot-3以上的版本,那么mybatis plus版本要用mybatis-plus-spring-boot3-starter
如下我这里就是使用的springboot-3.2.7
代码在此,请自取
<!-- mybatis -->
<!-- <dependency>-->
<!-- <groupId>org.mybatis.spring.boot</groupId>-->
<!-- <artifactId>mybatis-spring-boot-starter</artifactId>-->
<!-- <version>${mybatis.version}</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>com.baomidou</groupId>-->
<!-- <artifactId>mybatis-plus-boot-starter</artifactId>-->
<!-- <version>${mybatis-plus.version}</version>-->
<!-- </dependency>-->
<!-- springboot整合 mybatis-plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-spring-boot3-starter</artifactId>
<version>3.5.6</version><!--如果你的项目没有用父级管理jar,这里必须要写引入的版本号,下面也是一样-->
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
</dependency>
<!--mybatis-plus 代码生成器-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
</dependency>