商城首页欢迎来到中国正版软件门户

您的位置: 首页 > 文章列表 > 编程开发 > SpringBoot所管理的依赖和需要的依赖冲突问题及解决

SpringBoot所管理的依赖和需要的依赖冲突问题及解决

  发布于2026-05-31 阅读(0)

扫一扫,手机访问

背景

用 Spring Boot 2.7.7 集成 Elasticsearch 7.9.3,启动直接报错,截图里这堆异常信息看得人头疼。

SpringBoot所管理的依赖和需要的依赖冲突问题及解决

Error creating bean with name 'client' defined in class path resource

[com/sgp/config/EsConfig.class]: Bean instantiation via factory method failed; nested exception

is org.springframework.beans.BeanInstantiationException: Failed to instantiate

[org.elasticsearch.client.RestHighLevelClient]: Factory method 'client' threw exception; nested

exception is java.lang.NoClassDefFoundError:

org/elasticsearch/common/xcontent/DeprecationHandler at

org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:658) ~[spring-beans-5.3.24.jar:5.3.24]

排查后发现,项目里除了我自己引入的 ES 7.9.3 的 jar 包,Spring Boot 2.7.7 默认还带了一份 7.13.1 版本的依赖。最初 pom.xml 是这样写的:



    4.0.0
    pom
    
        orders
    
    
        org.springframework.boot
        spring-boot-starter-parent
        2.7.7
         
    
    com.sgp
    es-study
    0.0.1-SNAPSHOT
    es-study
    es-study
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter
        
        
            org.elasticsearch.client
            elasticsearch-rest-high-level-client
            7.9.3
        
        .....

解决方案

网上有些说法是,直接在 里指定版本就能覆盖 Spring Boot 管理的依赖版本。但实际试下来,这种方式在我这个项目里并没有生效。问题出在 Spring Boot 父 POM 已经通过 锁定了 ES 的版本,单纯的 7.9.3 声明优先级不够,最终被父 POM 的版本覆盖了。

真正的解决办法是:在 里显式声明一个版本属性,然后在 里用 ${} 引用它。这样能确保子模块的版本配置生效,覆盖掉父 POM 的默认版本。修改后的 pom 核心片段如下(先记录这个方案,后面找到根本原因再深入分析):



    4.0.0
    pom
    
        orders
    
    
        org.springframework.boot
        spring-boot-starter-parent
        2.7.7
         
    
    com.sgp
    es-study
    0.0.1-SNAPSHOT
    es-study
    es-study
    
        1.8
        7.9.3
    
    
        
            org.springframework.boot
            spring-boot-starter
        
        
            org.elasticsearch.client
            elasticsearch-rest-high-level-client
            ${elasticsearch.version}
        
.....

总结

当 Spring Boot 管理的依赖与你手动引入的版本冲突时,直接写死版本号未必有效——因为父 POM 的 优先级更高。最佳实践是在 中定义自定义版本属性,再通过 ${} 引用,这样才能稳稳覆盖。遇到类似问题不妨按这个思路排查一下。

本文转载于:https://www.jb51.net/program/364869ije.htm 如有侵犯,请联系zhengruancom@outlook.com删除。
免责声明:正软商城发布此文仅为传递信息,不代表正软商城认同其观点或证实其描述。

热门关注