1. 1 版本依赖管理
使用Spring Boot版本依赖管会有两个疑问:
- 为什么导入dependency是不需要指定版本;
- Spring-boot-starter-parent父依赖启动器的主要作用是进行版本统一管理,那么项目运行依赖的JAR包是从何而来的;
1.1.1. SpringBoot工程导入dependency不需要指定版本
在Spring Boot项目中的pom.xml文件中找到spring-boot-starter-parent依赖,示例如下:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
在spring boot项目中,将spring-boot-starter-parent依赖作为Spring Boot项目的统一父项目依赖管理,并将项目版本号统一为2.2.9.RELEASE,该版本号根据实际开发需求是可以修改的
点击进入查看spring-boot-starter-parent底层源文件,先看spring-boot-starter-parent做了哪些事
- 首先看spring-boot-starter-parent的properties节点
<properties>
<main.basedir>${basedir}/../../..</main.basedir>
<java.version>1.8</java.version>
<resource.delimiter>@</resource.delimiter> <!-- delimiter that doesn't clash with Spring ${} placeholders -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
</properties>
在spring-boot-starter-parent中的properties标签中主要定义了以下几点:
- 工程的Java版本为1.8;
- 工程代码的编译源文件编码格式为UTF-8;
- 工程编译后的文件编码格式为UTF-8;
- Maven打包编译的版本。
- 再来看spring-boot-starter-parent的build节点
在POM的build节点,分别定义了resources资源和pluginManagement
<!-- Turn on filtering by default for application properties -->
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/application*.yml</include>
<include>**/application*.yaml</include>
<include>**/application*.properties</include>
</includes>
</resource>
<resource>
<directory>${basedir}/src/main/resources</directory>
<excludes>
<exclude>**/application*.yml</exclude>
<exclude>**/application*.yaml</exclude>
<exclude>**/application*.properties</exclude>
</excludes>
</resource>
</resources>
详细看一下resources节点,里面定义了资源过滤,针对application的yml、properties格式进行了过滤,可以支持不同环境的配置,比如application-dev.yml、application-test.yml、application-dev.properties、application-test.properties等等。
pluginManagement则是引入了相应的插件和对应的版本依赖
<pluginManagement>
<plugins>
<!-- Apply more sensible defaults for user projects -->
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<configuration>
<jvmTarget>${java.version}</jvmTarget>
<javaParameters>true</javaParameters>
</configuration>
. . . . . .
- 最后来看spring-boot-starter-parent的父依赖spring-boot-dependencies
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${revision}</version>
<relativePath>../../spring-boot-dependencies</relativePath>
</parent>
spring-boot-dependencies的properties节点,可以看到spring-boot-dependencies才是SpringBoot项目真正管理依赖的项目,里面定义了SpringBoot相关版本
<properties>
<main.basedir>${basedir}/../..</main.basedir>
<!-- Dependency versions -->
<activemq.version>5.15.13</activemq.version>
<antlr2.version>2.7.7</antlr2.version>
<appengine-sdk.version>1.9.81</appengine-sdk.version>
<artemis.version>2.10.1</artemis.version>
<aspectj.version>1.9.6</aspectj.version>
<assertj.version>3.13.2</assertj.version>
<atomikos.version>4.0.6</atomikos.version>
<awaitility.version>4.0.3</awaitility.version>
<bitronix.version>2.1.4</bitronix.version>
<byte-buddy.version>1.10.13</byte-buddy.version>
. . . . . .
</properties>
spring-boot-dependencies的dependencyManagement节点中的dependencies定义了SpringBoot版本依赖的组件以及相应的版本
<dependencyManagement>
<dependencies>
<!-- Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test-autoconfigure</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator</artifactId>
<version>${revision}</version>
</dependency>
. . . . . .
</dependencies>
</dependencyManagement>
spring-boot-starter-parent通过继承spring-boot-dependencies从而实现了SpringBoot的版本依赖管理,所以我们的SpringBoot工程继承spring-boot-starter-parent后已经具备版本锁定等配置了,这也就是在SpringBoot项目中部分依赖不需要写版本号的原因。
1.1.2. SpringBoot依赖的Jar包从何而来
我们使用SpringBoot构建一个web项目,在该项目中只需要引入一个spring-boot-starter-web的依赖即可,我们不需要再去关注tomcat、SpringMVC等框架了,查看spring-boot-starter-web依赖文件的源码,核心代码具体如下:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-el</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
</dependencies>
从上述代码可以发现,spring-boot-starter-web依赖启动器的主要作用是打包了Web开发场景所需的底层所有依赖。正式如此,在pom.xml中引入spring-boot-starter-web依赖启动器时,就可以实现Web场景开发,而不需要额外导入tomcat服务器以及其他Web依赖文件等。当然,这些引入的依赖文件的版本号还是由spring-boot-starter-parent父依赖进行的统一管理。
SpringBoot除了提供有上述介绍的Web依赖启动器外,还提供了其他许多开发场景的相关依赖,可以打开SpringBoot官方文档,搜索“Starters”关键字查询场景依赖启动器。
列出了SpringBoot官方提供的部分场景依赖启动器,这些依赖启动器适用于不同开发场景,使用时只需要在pom.xml文件中导入对应的依赖启动器即可。
需要说明的是,Spring Boot官方并不是针对所有场景开发的技术框架都提供了场景启动器,例如阿里 巴巴的Druid数据源等,Spring Boot官方就没有提供对应的依赖启动器。为了充分利用Spring Boot框 架的优势,在Spring Boot官方没有整合这些技术框架的情况下,Druid等技术框架所在的开发团队主动 与Spring Boot框架进行了整合,实现了各自的依赖启动器,例如druid-spring-boot-starter等。我们在 pom.xml文件中引入这些第三方的依赖启动器时,切记要配置对应的版本号