解决方案
解决jar包冲突的方式一
先声明的jar包坐标下的依赖包,可以优先进入项目
解决jar包冲突方式二:
maven导入jar包中的一些概念
什么是直接依赖,项目中直接导入的jar包就是改项目的直接依赖包
传递依赖:
项目中没有直接导入的jar包,可以通过项目直接依赖传递到项目中
路径近者优先原则,直接依赖路径比传递依赖路径近,
那么最终项目进入jar会是直接导入的jar包
1.传递依赖
会发现出现除了 spring-webmvc 以外的其他 jar。因为我们的项目依赖 spring-webmv.jar,而spring-webmv.jar 会依赖 spring-beans.jar 等等,所以 spring-beans.jar 这些 jar 包也出现在了我们的 maven 工程中,这种现象我们称为依赖递。
例如:在实际中但是spring-webmvc 依赖 spirng-beans-4.2.4,spring-context 依赖 spring-beans-5.0.2,但是发现spirng-beans-4.2.4 加入到工程中
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
</dependencies>
2.路径近者优先原则
例如:还是上述情况,spring-contex 和 spring-webmvc 都会传递过来 spirng-beans,那如果直接把 spring-beans 的依赖直接写到 pom 文件中,那么项目就不会再使用其他依赖传递来的 spring-beans,因为自己直接在 pom 中定义 spring-beans 要比其他依赖传递过来的路径要近。
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-bean</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
</dependencies>
解决jar包方式三
直接排除法
当我们要排除某个jar包下依赖包,在配置exckusions标签时候,内部可以不写版本号
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.2.4.RELEASE</version>
<!-- 此处直接排除spring-core -->
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.13.RELEASE</version>
</dependency>
</dependencies>