在Maven项目中,不同的第三方依赖包可以直接或者间接依赖于Spring,这些Spring的版本各有不同,则在项目中容易产生版本的冲突。
Spring不同模块或者与外部进行集成时,依赖处理就需要各自对应版本号。它们的集成经常会遇到问题,给搭建和升级带来不便。
Spring-framework-bom是由Maven提供的功能,用以统一间接或者直接依赖的类库版本,强制某个类库使用某一个统一的版本。
SpringSource为了解决这些Jar冲突,推出了各种BOM,最著名的就是spring platform io bom,其中最核心的三个是:spring-framework-bom、spring-boot-dependencies、platform-bom。
在maven的pom.xml中无需指定具体的类库版本,直接使用,即默认使用bom中指定的版本
。
Spring-framework-bom.jar 作用
Maven “Bill Of Materials
” Dependency
It is possible to accidentally mix different versions of Spring JARs when using Maven. For example,
you may find that a third-party library, or another Spring project, pulls in a transitive dependency to an
older release. If you forget to explicitly declare a direct dependency yourself, all sorts of unexpected
issues can arise.
To overcome such problems Maven supports the concept of a “bill of materials” (BOM) dependency.
You can import the spring-framework-bom in your dependencyManagement section to ensure
that all spring dependencies (both direct and transitive) are at the same version.
Maven“物料清单”依赖
使用 Maven 时可能会意外混合不同版本的 Spring JAR。 例如,
您可能会发现第三方库或另一个 Spring 项目将传递依赖项拉入到
旧版本。 如果您忘记自己显式声明直接依赖项,则会出现各种意想不到的情况
可能会出现问题。
为了克服这些问题,Maven 支持“材料清单”(BOM)依赖的概念。
您可以在您的 dependencyManagement 部分导入 spring-framework-bom 以确保
所有 spring 依赖项(直接和传递)都在同一版本。
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>${spring.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
时,所有使用的spring模块dependency都不需要在声明version属性。例如:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>