发布于2026-05-31 阅读(0)
扫一扫,手机访问
用 Spring Boot 2.7.7 集成 Elasticsearch 7.9.3,启动直接报错,截图里这堆异常信息看得人头疼。

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 的版本,单纯的 声明优先级不够,最终被父 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 的 优先级更高。最佳实践是在 中定义自定义版本属性,再通过 ${} 引用,这样才能稳稳覆盖。遇到类似问题不妨按这个思路排查一下。
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
正版软件
正版软件
正版软件
正版软件
正版软件
1
2
3
7
8