1. 程式人生 > >Spring報錯 Failed to read candidate component class: file Caused by: java.lang.IncompatibleClassChange

Spring報錯 Failed to read candidate component class: file Caused by: java.lang.IncompatibleClassChange

今天寫一個SpringMVC的小例子,遇到一個很怪異的問題。

先說說開發環境:IDEA+SpringMVC+Gradle
先看看我的build.gradle檔案引入的包:

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    compile group: 'org.springframework', name: 'spring-core', version: '5.0.5.RELEASE'
    compile group: 'org.springframework'
, name: 'spring-beans', version: '5.0.5.RELEASE' compile group: 'org.springframework', name: 'spring-context', version: '5.0.5.RELEASE' compile group: 'org.springframework', name: 'spring-expression', version: '5.0.5.RELEASE' compile group: 'org.springframework', name: 'spring-web', version: '5.0.5.RELEASE'
compile group: 'org.springframework', name: 'spring-webmvc', version: '5.0.5.RELEASE' compile group: 'org.springframework', name: 'spring-aop', version: '5.0.5.RELEASE' compile group: 'org.springframework', name: 'spring-asm', version: '3.1.4.RELEASE' }

執行專案報錯如下:

org.springframework.beans.factory.BeanDefinitionStoreException:
Failed to read candidate component class: file nested exception is java.lang.IncompatibleClassChangeError: class org.springframework.core.type.classreading.ClassMetadataReadingVisitor has interface org.springframework.asm.ClassVisitor as super class .... Caused by: java.lang.IncompatibleClassChangeError: class org.springframework.core.type.classreading.ClassMetadataReadingVisitor has interface org.springframework.asm.ClassVisitor as super class ....

這裡只列出關鍵的報錯資訊,其他資訊就不貼出來了。搜尋了一番發現原因如下:

  1. spring-asm版本和spring-core版本不一致導致。
  2. spring在4.x之後把spring-asm整合進了spring-core中。
    好吧,然後我就把build.gradle中的asm的依賴也就是下面這行刪除了。
compile group: 'org.springframework', name: 'spring-asm', version: '3.1.4.RELEASE'

然後執行依然報錯,這個就很詭異了,最終發現,即使你在build.gradle中移除了這一行,spring-asm這個jar包仍然在你的專案裡面,你必須徹底刪除。
如下,我在專案目錄下搜尋jar結尾的檔案:
這裡寫圖片描述
是不是很尷尬,搜尋到刪除asm的jar包即可解決問題。
希望這個方法可以幫助到大家。