7月 18, 2024 9:27:53 上午 org.springframework.context.support.AbstractApplicationContext refresh
WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: file [D:\Users\F0842965\jdk-17.0.11\webprojects\spring_mybatis\target\classes\com\leo\App.class]; nested exception is org.springframework.core.NestedIOException: ASM ClassReader failed to parse class file - probably due to a new Java class file version that isn't supported yet: file [D:\Users\F0842965\jdk-17.0.11\webprojects\spring_mybatis\target\classes\com\leo\App.class]; nested exception is java.lang.IllegalArgumentException: Unsupported class file major version 61
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class:
错误信息表明你使用的Spring框架不支持由JDK 17.0.11生成的Java类文件格式。这个问题源于不受支持的类文件主版本号(61),这对应于Java 17。Spring在5.3版本之前并不官方支持Java 17。
为了解决这个问题,你可以采取以下一个或多个步骤:
-
升级Spring框架:
确保你的Spring框架版本与Java 17兼容。从Spring Framework 5.3.x及以后的版本开始,它支持Java 17。检查你的pom.xml
或build.gradle
文件,并相应地更新Spring依赖项。 -
降级JDK:
如果升级Spring不可行,可以考虑将JDK降级到与你当前Spring设置兼容的版本。例如,Java 8(主版本52)或Java 11(主版本55)可能有效,如果这些版本与你的Spring版本兼容的话。 -
更新其他依赖项:
有时候项目中的其他库或工具也需要更新以支持更新的Java版本。确保构建过程的所有组件(如Maven、Gradle以及任何插件)都是最新的并且能够处理Java 17。 -
检查构建设置:
验证IDE(如IntelliJ IDEA或Eclipse)或构建脚本中的构建设置是否正确设置为目标JDK版本。
以下是Maven项目中如何更新Spring框架版本的方法:
在pom.xml
中添加或更新依赖项:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.20</version>
</dependency>