问题描述
youlai-boot 升级 Spring Boot 3.2 版本项目启动报错:
java.lang.IllegalArgumentException: Invalid value type for attribute 'factoryBeanObjectType': java.lang.String
报错截图如下:
原因分析
mybatis-spring 官方 ISSUE: https://github.com/mybatis/spring/issues/855
项目中使用 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 类型,不符合条件而抛出异常。
解决方案(新)
Mybatis-Plus 于 2023年12月24日发布 mybatis-plus v3.5.5 版本,发布日志声明 升级spring-boot3版本mybatis-spring至3.0.3。
所以升级 Mybatis-Plus 版本为 3.5.5 版本即可,需要注意下 Maven 的坐标标识 是mybatis-plus-spring-boot3-starter,这点和SpringBoot 2 的依赖坐标mybatis-plus-boot-starter有所区别。
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-spring-boot3-starter</artifactId>
<version>3.5.5</version>
</dependency>
解决方案(旧)
mybatis-spring 官方 ISSUE 说明在 3.0.3 版本修复此问题
确实 3.0.3 版本已出
Mybatis-Plus 官方 ISSUE#5808 下面也说明会在 3.5.5 版本升级 mybatis-spring 依赖修复此问题,但截止到目前只有快照版本 3.5.5-SNAPSHOT 。
所以目前好一点的方案就是手动升级 mybatis-spring 版本为 3.0.3
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.4.1</version>
<exclusions>
<exclusion>
<artifactId>mybatis-spring</artifactId>
<groupId>org.mybatis</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>3.0.3</version>
</dependency>
修改后再重新启动ok