解决 SpringBoot 升级 3.3.0 MybatisPlus报错 Invalid value type for attribute 'factoryBeanObjectType': java.lang.String
2024-06-14
阅读 {{counts.readCount}}
评论 {{counts.commentCount}}
## 前言
准备研究一下虚拟线程相关
SpringBoot + MybatisPlus项目
升级了一下JDK和各种依赖
SpringBoot 3.1.5 => 3.3.0
MybatisPlus 3.5.3.1 => 3.5.7
...
启动后出现如下错误,本篇记录下如何解决的报错
```java
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v3.3.0)
2024-06-14T13:40:26.165+08:00 INFO 12764 --- [ main] com.zzzmh.DemoApplication : Starting DemoApplication using Java 21.0.3 with PID 12764 (C:\mywork\idea-2023-workspace\demo\target\classes started by Administrator in C:\mywork\idea-2023-workspace\demo)
2024-06-14T13:40:26.167+08:00 DEBUG 12764 --- [ main] com.zzzmh.DemoApplication : Running with Spring Boot v3.3.0, Spring v6.1.8
2024-06-14T13:40:26.168+08:00 INFO 12764 --- [ main] com.zzzmh.DemoApplication : The following 1 profile is active: "dev"
2024-06-14T13:40:27.128+08:00 INFO 12764 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode
2024-06-14T13:40:27.131+08:00 INFO 12764 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data Redis repositories in DEFAULT mode.
2024-06-14T13:40:27.188+08:00 INFO 12764 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 39 ms. Found 0 Redis repository interfaces.
2024-06-14T13:40:27.348+08:00 WARN 12764 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: java.lang.IllegalArgumentException: Invalid value type for attribute 'factoryBeanObjectType': java.lang.String
2024-06-14T13:40:27.356+08:00 INFO 12764 --- [ main] .s.b.a.l.ConditionEvaluationReportLogger :
Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2024-06-14T13:40:27.380+08:00 ERROR 12764 --- [ main] o.s.boot.SpringApplication : Application run failed
java.lang.IllegalArgumentException: Invalid value type for attribute 'factoryBeanObjectType': java.lang.String
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getTypeForFactoryBeanFromAttributes(FactoryBeanRegistrySupport.java:86) ~[spring-beans-6.1.8.jar:6.1.8]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getTypeForFactoryBean(AbstractAutowireCapableBeanFactory.java:837) ~[spring-beans-6.1.8.jar:6.1.8]
at org.springframework.beans.factory.support.AbstractBeanFactory.isTypeMatch(AbstractBeanFactory.java:663) ~[spring-beans-6.1.8.jar:6.1.8]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBeanNamesForType(DefaultListableBeanFactory.java:575) ~[spring-beans-6.1.8.jar:6.1.8]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:534) ~[spring-beans-6.1.8.jar:6.1.8]
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:138) ~[spring-context-6.1.8.jar:6.1.8]
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:788) ~[spring-context-6.1.8.jar:6.1.8]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:606) ~[spring-context-6.1.8.jar:6.1.8]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) ~[spring-boot-3.3.0.jar:3.3.0]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754) ~[spring-boot-3.3.0.jar:3.3.0]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:456) ~[spring-boot-3.3.0.jar:3.3.0]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:335) ~[spring-boot-3.3.0.jar:3.3.0]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1363) ~[spring-boot-3.3.0.jar:3.3.0]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1352) ~[spring-boot-3.3.0.jar:3.3.0]
at com.zzzmh.DemoApplication.main(DemoApplication.java:10) ~[classes/:na]
Process finished with exit code 1
```
## 折腾
稍微百度了一下,就这位老哥说的最准确
https://www.jb51.net/program/308745bxt.htm
顺便贴一下Github上的相关的issues
https://github.com/baomidou/mybatis-plus/issues/5962
https://github.com/baomidou/mybatis-plus/issues/5747
https://github.com/mybatis/spring/issues/855
总结:
(以下内容直接摘抄自jb51作者有来技术)
>项目中使用 mybatis-plus-boot-starter 当前最新版本 3.5.4.1 ,其中依赖的 mybatis-spring 版本为 2.1.1
在 mybatis-spring 2.1.1 版本的 ClassPathMapperScanner#processBeanDefinitions 方法里将 BeanClassName 赋值给 String 变量
并将 beanClassName 赋值给 factoryBeanObjectType
但是在 Spring Boot 3.2 版本中FactoryBeanRegistrySupport#getTypeForFactoryBeanFromAttributes方法已变更,如果 factoryBeanObjectType 不是 ResolvableType 或 Class 类型会抛出 IllegalArgumentException 异常。
此时因为 factoryBeanObjectType 是 String 类型,不符合条件而抛出异常。
<br>
简而言之
项目中直接或间接依赖 `mybatis-spring 2.x.x` 的,不能兼容 `Springboot 3.2.0` 及以上版本
<br>
现在存在一个时间点上的尴尬,截止2024年6月,MyBatisPlus 3.5.7版本中,仍然在间接依赖 `mybatis-spring 2.1.2`,官方尚未更新到 `mybatis-spring 3.x.x`,这到也不怪MyBatisPlus,因为祸是mybatis闯的
<br>
所以解决方案就3个
1. 要么Springboot 退到3.1.x 版本,不支持虚拟线程
2. 要么手动剔除`mybatis-spring 2.1.2` 强制改为 `mybatis-spring 3.x.x`
3. 要么等,等MybatisPlus更新到依赖 `mybatis-spring 3.x.x` 的版本
<br>
方案2的代码如下
`pom.xml`
```xml
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.7</version>
<exclusions>
<exclusion>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>3.0.3</version>
</dependency>
```
大功告成
## END