SpringBoot Web项目依赖分析

在上篇中,我们得到如下的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.3.RELEASE</version>
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

看看其核心类容:

  • 1.parent
   <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
  • 2.dependencies
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

按住ctrl并点击parent中spring-boot-stater-parent节点,可以看到


图片.png

spring-boot-stater-parent有一个parent spring-boot-dependencies
根据名字分析,是spring-boot项目依赖的
继续点击spring-boot-dependencies
可以看到其在properties中配置了大量的依赖版本

![图片.png](https://upload-images.jianshu.io/upload_images/15204427-d5adbee2905a628c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

我当前这个版本(2.2.3.RELEASE)在properties中共配置了203个依赖的版本
properties下面 dependencyManagement节点中,配置了依赖组建的版本:


图片.png

在项目的pom.xml中查看依赖树:


图片.png

可以看到springboot-starter-web添加了tomcat,web,webmvc,spring-core,spring-context等依赖
这也就是为什么我们可以直接通过DemoApplication.main来运行,而不需要配置外部servlet容器的原因,同时,通过这个starter-web就已经将web相关的依赖都整合进来了。

springboot中提供了很多starter,比如

  • spring-boot-starter-web
  • spring-boot-starter-cache
  • spring-boot-starter-security
  • spring-boot-starter-jdbc-swagger
  • spring-boot-starter-data-xxx
    以上spring-boot-starter-开头的都是官方支持的,这些依赖在导入的时候,无需指定版本,因为他们的版本已经在spring-boot-dependencies中指定了
    还有一些第三方的:
    例如mybatis-spring-boot-starter,这种以-spring-boot-starter结尾的,是第三方根据springboot的规范,实现了第三方框架与springboot的整合,引入依赖时需要指定版本,因为其版本管理不包含在spring-boot-dependencies中。
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容