SpringBoot所管理的依赖和需要的依赖冲突解决方案

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]

查看后发现当前项目里面不仅有我引入的 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>
        .....

解决方案

网上有的说像我以上的这种写的指定依赖版本 就可以覆盖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>
.....
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot应用中,如果出现了依赖注入冲突,一般可以采取以下几种解决方案: 1. 排除依赖:可以在项目的pom.xml文件中,对冲突依赖进行排除,例如: ``` <dependency> <groupId>com.example</groupId> <artifactId>example</artifactId> <version>1.0.0</version> <exclusions> <exclusion> <groupId>com.example</groupId> <artifactId>example-conflict</artifactId> </exclusion> </exclusions> </dependency> ``` 在上述代码中,我们排除了com.example:example依赖中的com.example:example-conflict依赖。 2. 指定依赖版本:如果冲突依赖版本不一致,可以在项目的pom.xml文件中,指定依赖版本,例如: ``` <dependency> <groupId>com.example</groupId> <artifactId>example</artifactId> <version>1.0.0</version> </dependency> <dependency> <groupId>com.example</groupId> <artifactId>example-conflict</artifactId> <version>2.0.0</version> </dependency> ``` 在上述代码中,我们指定了com.example:example依赖版本为1.0.0,com.example:example-conflict依赖版本为2.0.0。 3. 使用Spring Boot提供的依赖管理Spring Boot提供了一些依赖管理功能,可以解决依赖冲突问题,例如: ``` <dependencyManagement> <dependencies> <dependency> <groupId>com.example</groupId> <artifactId>example</artifactId> <version>1.0.0</version> </dependency> <dependency> <groupId>com.example</groupId> <artifactId>example-conflict</artifactId> <version>2.0.0</version> </dependency> </dependencies> </dependencyManagement> ``` 在上述代码中,我们在dependencyManagement中指定了com.example:example和com.example:example-conflict的版本,这样在其他地方引用这两个依赖时,就不会出现版本冲突问题了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值