SpringBoot所管理的依賴和需要的依賴沖突問題及解決
背景
在使用springboot2.7.7 集成 elasticsearch7.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]
查看后發(fā)現(xiàn)當前項目里面不僅有我引入的 es 7.9.3 版本的jar 還有springboot 2…7.7 所默認加入7.13.1版jar,最初我的pom是這樣的
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging>
<modules>
<module>orders</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.7</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.sgp</groupId>
<artifactId>es-study</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>es-study</name>
<description>es-study</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.9.3</version>
</dependency>
.....解決方案
網(wǎng)上有的說像我以上的這種寫的指定依賴版本 就可以覆蓋springboot所管理的依賴,但很明顯在我這里并沒有用,于是通過在 <properties></properties> 里加上指定版本 然后再 dependency 中通過${}引入解決掉的 (先記錄一下 后面找到為什么的時候在寫原因)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging>
<modules>
<module>orders</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.7</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.sgp</groupId>
<artifactId>es-study</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>es-study</name>
<description>es-study</description>
<properties>
<java.version>1.8</java.version>
<elasticsearch.version>7.9.3</elasticsearch.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
.....總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Java調(diào)用第三方http接口的常用方式總結(jié)
這篇文章主要介紹了Java調(diào)用第三方http接口的常用方式總結(jié),具有很好的參考價值,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06
springboot調(diào)用python文件的詳細方案
這篇文章主要為大家詳細介紹了springboot調(diào)用python文件的詳細方案,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下2025-04-04
如何使用BeanUtils.copyProperties進行對象之間的屬性賦值
這篇文章主要介紹了使用BeanUtils.copyProperties進行對象之間的屬性賦值,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-05-05
Spring使用RestTemplate模擬form提交示例
本篇文章主要介紹了Spring使用RestTemplate模擬form提交示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03
SpringBoot 二維碼生成base64并上傳OSS的實現(xiàn)示例
本文主要介紹了SpringBoot 二維碼生成base64并上傳OSS的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-05-05

