首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計疑難解答

Spring 整合 MyBatis 報錯 MalformedParameterizedTypeException 的版本衝突排查與解決

     編輯:程式設計疑難解答

異常堆棧

Caused by: java.lang.reflect.MalformedParameterizedTypeException
    at sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl.validateConstructorArguments...
    at org.springframework.beans.CachedIntrospectionResults.<init>(CachedIntrospectionResults.java:284)
    at org.springframework.beans.BeanWrapperImpl.getCachedIntrospectionResults...

根本原因

Spring 的反射工具 CachedIntrospectionResults 在解析泛型類型(如 Mapper<T>)時,遇到 MalformedParameterizedTypeException,通常是因為 Spring 版本與 MyBatis-Spring 版本不兼容,導致泛型簽名解析失敗。

常見不兼容組合

  • Spring 4.x + MyBatis-Spring 1.x(舊版)
  • Spring 3.2.x + MyBatis-Spring 2.x(新版)

解決方案

  1. 統一升級:Spring 4.3.x + MyBatis 3.4.x + MyBatis-Spring 1.3.x(穩定組合)。
  2. 或降級:Spring 3.2.x + MyBatis 3.2.x + MyBatis-Spring 1.2.x(舊項目維護)。
  3. 檢查依賴傳遞:Maven dependency:tree 確認無重複/衝突 jar。

Maven 推薦坐標

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.3.25.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.4.6</version>
</dependency>
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>1.3.2</version>
</dependency>

排查技巧

  • 異常拋出於 Bean 初始化階段,說明配置文件加載無誤,問題在實例化過程中的反射解析。
  • 若版本正確仍報錯,檢查是否有多版本 spring-core、spring-beans 混雜。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved