Spring 5.2.20.RELEASE升级后Bean创建失败问题求助
Spring 3.x升级到5.2.20.RELEASE启动报错排查方案
报错信息
SEVERE: Servlet.init() for servlet [appServlet] threw exception org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping': Instantiation of bean failed; nested exception is java.lang.NoSuchMethodError: org.springframework.core.annotation.AnnotatedElementUtils.findMergedAnnotation(Ljava/lang/reflect/AnnotatedElement;Ljava/lang/Class;)Ljava/lang/annotation/Annotation; ...(完整报错堆栈见原文)
问题分析
这个NoSuchMethodError本质是Spring Core模块版本不兼容:AnnotatedElementUtils.findMergedAnnotation()方法是Spring Core 4.2及以上版本才新增的API,升级到Spring 5.2.20.RELEASE后,若项目中仍存在低版本的spring-core依赖(比如Spring 3.x版本的jar包),就会导致JVM加载到旧版本类,找不到目标方法。
解决步骤
排查依赖冲突:执行Maven命令查看依赖树,定位所有
spring-core的版本:mvn dependency:tree | grep spring-core确保输出结果中所有
spring-core的版本均为5.2.20.RELEASE,无低版本依赖混入。统一Spring版本管理:在
pom.xml的<dependencyManagement>节点中锁定所有Spring模块的版本,避免版本不一致:<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.2.20.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.20.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.20.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>5.2.20.RELEASE</version> </dependency> <!-- 其他用到的Spring模块均需统一版本 --> </dependencies> </dependencyManagement>清理依赖缓存:执行命令强制更新依赖,清理本地仓库中可能存在的旧版本jar包:
mvn clean install -U处理第三方依赖冲突:若某些第三方库依赖旧版本Spring,需通过
<exclusions>排除其自带的低版本Spring Core:<dependency> <groupId>第三方库GroupId</groupId> <artifactId>第三方库ArtifactId</artifactId> <version>对应版本号</version> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> </exclusion> <!-- 若有其他低版本Spring模块也需排除 --> </exclusions> </dependency>
内容的提问来源于stack exchange,提问作者soorka Ravi
相关产品推荐
相关产品推荐

