Spring Boot & MyBatis Plus 版本兼容问题(Invalid value type for attribute 'factoryBeanObjectType': java.lang.String)
- 问题描述
- 问题排查
- 1. 检查 `@MapperScan` 的路径
- 2. 项目中没有配置 `FactoryBean`
- 3. 检查 `Spring` 和 `MyBatis Plus` 版本兼容性
- 解决方法
问题描述
当使用 spring-boot-starter-parent
v3.2.0
以上版本;mybatis-plus-boot-starter
3.5.10
时,如:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.10</version>
</dependency>
会出现以下错误:
Invalid value type for attribute 'factoryBeanObjectType': java.lang.String
错误详情:
org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'userMapper' defined in file [/Volumes/Macintosh/Workspace/Michael/michael-spica-mybatis-plus/target/classes/michael/spica/mybatis/plus/mapper/UserMapper.class]: Invalid value type for attribute 'factoryBeanObjectType': java.lang.String
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getTypeForFactoryBean(AbstractAutowireCapableBeanFactory.java:858) ~[spring-beans-6.2.1.jar:6.2.1]
at org.springframework.beans.factory.support.AbstractBeanFactory.getType(AbstractBeanFactory.java:742) ~[spring-beans-6.2.1.jar:6.2.1]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAnnotationOnBean(DefaultListableBeanFactory.java:765) ~[spring-beans-6.2.1.jar:6.2.1]
at org.springframework.boot.sql.init.dependency.AnnotationDependsOnDatabaseInitializationDetector.detect(AnnotationDependsOnDatabaseInitializationDetector.java:36) ~[spring-boot-3.4.1.jar:3.4.1]
at org.springframework.boot.sql.init.dependency.DatabaseInitializationDependencyConfigurer$DependsOnDatabaseInitializationPostProcessor.detectDependsOnInitializationBeanNames(DatabaseInitializationDependencyConfigurer.java:152) ~[spring-boot-3.4.1.jar:3.4.1]
at org.springframework.boot.sql.init.dependency.DatabaseInitializationDependencyConfigurer$DependsOnDatabaseInitializationPostProcessor.postProcessBeanFactory(DatabaseInitializationDependencyConfigurer.java:115) ~[spring-boot-3.4.1.jar:3.4.1]
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:363) ~[spring-context-6.2.1.jar:6.2.1]
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:197) ~[spring-context-6.2.1.jar:6.2.1]
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:791) ~[spring-context-6.2.1.jar:6.2.1]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:609) ~[spring-context-6.2.1.jar:6.2.1]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) ~[spring-boot-3.4.1.jar:3.4.1]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:752) ~[spring-boot-3.4.1.jar:3.4.1]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:439) ~[spring-boot-3.4.1.jar:3.4.1]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:318) ~[spring-boot-3.4.1.jar:3.4.1]
at michael.spica.mybatis.plus.MichaelSpicaMybatisPlusApplication.main(MichaelSpicaMybatisPlusApplication.java:42) ~[classes/:na]
问题排查
1. 检查 @MapperScan
的路径
@MapperScan
注解中提供的包路径正确,并且能够扫描到 UserMapper 类;路径不正确可能导致 Spring 不能正确地创建和管理 Mapper Bean。
@SpringBootApplication
@MapperScan("michael.spica.mybatis.plus.mapper")
public class MichaelSpicaMybatisPlusApplication {
public static void main(String[] args) {
SpringApplication.run(MichaelSpicaMybatisPlusApplication.class, args);
}
}
2. 项目中没有配置 FactoryBean
从错误信息来看,factoryBeanObjectType
设置为了 String 类型,而应该是 Class
类型。
MyBatis 会自动生成 Mapper 接口的代理实现对象,而你不应该手动配置 FactoryBean
。
@Bean
public MapperFactoryBean<UserMapper> userMapper() {
MapperFactoryBean<UserMapper> factoryBean = new MapperFactoryBean<>(UserMapper.class);
factoryBean.setFactoryBeanObjectType(String.class); // 错误配置,应该使用 Class 类型
return factoryBean;
}
上面的 setFactoryBeanObjectType(String.class)
是错误的配置,应该删除这段配置,让 MyBatis 自动处理 Mapper 的代理对象。
正确方式:
MyBatis 和 MyBatis Plus 会自动处理 Mapper 的代理,因此一般不需要手动配置FactoryBean
。
3. 检查 Spring
和 MyBatis Plus
版本兼容性
当使用的 Spring Boot 3.2.0+(含v3.2.0)
和 MyBatis Plus 3.5.10
时,会出现错误;所以经排查,是 Spring
和 MyBatis Plus
版本兼容性问题。
解决方法
降低 Spring Boot
的版本至 3.2.0 以下,如:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.12</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
即可解决问题 ~~
完整pom文件,如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.12</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>michael.spica.mybatis.plus</groupId>
<artifactId>michael-spica-mybatis-plus</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>michael-spica-mybatis-plus</name>
<description>michael-spica-mybatis-plus</description>
<properties>
<java.version>17</java.version> <!-- 定义项目使用的JDK版本 -->
<encoding>UTF-8</encoding> <!-- 定义项目编码格式 -->
<project.build.sourceEncoding>${encoding}</project.build.sourceEncoding> <!-- 源代码的编码方式 -->
<project.reporting.outputEncoding>${encoding}</project.reporting.outputEncoding> <!-- 输出报告编码方式 -->
<skipTests>true</skipTests> <!-- mvn打包时,跳过所有的测试阶段(包括编译和执行);也可以这样设置,如:<maven.test.skip>true</maven.test.skip> -->
<maven.compiler.parameters>true</maven.compiler.parameters> <!-- 开启JDK参数 -->
<maven.compiler.source>${java.version}</maven.compiler.source> <!-- 设置JDK版本(即:配置 Maven 编译时使用的源代码的 Java 版本) -->
<maven.compiler.target>${java.version}</maven.compiler.target> <!-- 设置JDK版本(即:配置编译生成的字节码版本为指定的 Java 版本) -->
<maven.build.timestamp.format>yyyy-MM-dd HH:mm:ss:SSS</maven.build.timestamp.format> <!-- 设置打包时间格式 -->
<!-- jars version
e.g: <mybatis-plus.version>x.x.x</mybatis-plus.version>
-->
<mybatis-plus.version>3.5.10</mybatis-plus.version>
<lombok.version>1.18.36</lombok.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
<!-- MySQL Connector Java
-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.34</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>3.4.1</version>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>