SpringBoot入门:依赖冲突的解决方案详解

SpringBootMaven补充依赖冲突什么是依赖冲突场景:spring-webmvc、spring-jdbc 都依赖 spring-beans spring-webmvc 的版本是5.2.8,那么它依赖的 spring-beans 的版本也是5.2.8 spring-jdbc 的版本是5.2.6,那么它依赖的 spring-beans 的版本也是5.2.6如果 spring-jdbc 和 spring-webmvc 放到一起,那么就会出现两个版本的 spring-beans ,
摘要由CSDN通过智能技术生成

SpringBoot

Maven补充

依赖冲突

什么是依赖冲突

场景:

  1. spring-webmvc、spring-jdbc 都依赖 spring-beans
  2. spring-webmvc 的版本是5.2.8,那么它依赖的 spring-beans 的版本也是5.2.8
  3. spring-jdbc 的版本是5.2.6,那么它依赖的 spring-beans 的版本也是5.2.6

如果 spring-jdbc 和 spring-webmvc 放到一起,那么就会出现两个版本的 spring-beans ,这样就产生了依赖冲突,那么久需要想办法只保留一个版本的 spring-beans

依赖冲突的解决方案①

默认情况下,优先保留前面先声明的版本

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.2.8.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.2.6.RELEASE</version>
</dependency>
复制代码

依赖冲突的解决方案②

单独为依赖库添加 dependency 指定版本

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>5.2.6.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.2.8.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.2.6.RELEASE</version>
</dependency>
复制代码

依赖冲突的解决方案③

使用 exclusion 排除某个依赖

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.2.8.RELEASE</version>
    <exclusions>
        <exclusion>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.2.6.RELEASE</version>
</dependency>
复制代码

依赖冲突的解决方案④(推荐)

使用 dependencyManagement 锁定依赖库的版本号

  • 注意: dependencyManagement 只是声明版本号,并不会触发下载依赖导入依赖库
<dependencyManagement>
    <dependencies>
       
  • 6
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值