springboot框架学习积累—SpringBoot源码剖析之依赖管理
1. 为什么导入dependency时不需要指定版本(spring-boot-starter-parent父依赖启动器进行版本统一管理)
Spring Boot程序中有两个核心的依赖spring-boot-starter-parent
,spring-boot-starter-web
-
spring-boot-starter-parent依赖
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent>
-
进入
spring-boot-starter-parent
并查看,发现底层有一个父依赖spring-boot-dependencies
,核心具体代码如下<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.2.2.RELEASE</version> <relativePath>../../spring-boot-dependencies</relativePath> </parent>
里面的properties标签
:定义了编码集的格式,jdk版本,maven编译版本<properties> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <resource.delimiter>@</resource.delimiter> <maven.compiler.source>${java.version}</maven.compiler.source> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.target>${java.version}</maven.compiler.target> </properties>
- 看下面
build
标签中includes
标签,引入外部配置文件,加载顺序是先yml文件
,后yaml文件
,最后properties文件
,所以说properties文件的优先级最高<resources> <resource> <filtering>true</filtering> <directory>${basedir}/src/main/resources</directory> <includes> <include>**/application*.yml</include> <include>**/application*.yaml</include> <include>**/application*.properties</include> </includes> </resource> ... </resources>
- 所以说,在SpringBoot的父工程中都做了什么?1. 进行编码的默认设置,JDK的默认版本设置,==2. == 引入一些配置文件,==3. == 插件管理
-
继续查看
spring-boot-dependencies
底层源码,核心代码如下,其中dependencyManagement
指定版本,进行版本的锁定,properties
只是声明版本<properties> <activemq.version>5.15.11</activemq.version> ... <solr.version>8.2.0</solr.version> <mysql.version>8.0.18</mysql.version> <kafka.version>2.3.1</kafka.version> <spring-amqp.version>2.2.2.RELEASE</spring-amqp.version> <spring-restdocs.version>2.0.4.RELEASE</spring-restdocs.version> <spring-retry.version>1.2.4.RELEASE</spring-retry.version> <spring-security.version>5.2.1.RELEASE</spring-security.version> <spring-session-bom.version>Corn-RELEASE</spring-session-bom.version> <spring-ws.version>3.0.8.RELEASE</spring-ws.version> <sqlite-jdbc.version>3.28.0</sqlite-jdbc.version> <sun-mail.version>${jakarta-mail.version}</sun-mail.version> <tomcat.version>9.0.29</tomcat.version> <thymeleaf.version>3.0.11.RELEASE</thymeleaf.version> <thymeleaf-extras-data-attribute.version>2.0.1 </thymeleaf-extras-dataattribute.version> ... </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot</artifactId> <version>2.2.9.RELEASE</version> </dependency> ... <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-amqp</artifactId> <version>${activemq.version}</version> </dependency> ... </dependencies> </dependencyManagement>
2. spring-boot-starter-parent父依赖启动器的主要作用是进行版本统一管理,那么项目运行jar包是从何而来(依赖传递)
- 查看
spring-boot-starter-webׁ
依赖文件源码发现,SpringBoot项目通过依赖spring-boot-starter-webׁ
会自动引入SpringMVC的依赖
,并且使用tomcat
作为内嵌容器,spring-boot-starter-webׁ
又依赖了web开发场景所需要的底层的所有依赖(spring-webmvc ...)
,通过依赖传递的方式来使用项目运行需要的jar
包<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <version>2.2.9.RELEASE</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-json</artifactId> <version>2.2.9.RELEASE</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <version>2.2.9.RELEASE</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> <version>2.2.9.RELEASE</version> <scope>compile</scope> <exclusions> <exclusion> <artifactId>tomcat-embed-el</artifactId> <groupId>org.apache.tomcat.embed</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>5.2.8.RELEASE</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.8.RELEASE</version> <scope>compile</scope> </dependency> </dependencies>