一看就喷亏的小猿

一看就喷亏的小猿

一、前言 

      传统我们搭建SSM项目的时候,使用maven做jar依赖管理的时候,还需要我们配置依赖jar包相应的版本,并且构建项目的时候,是需要什么jar包就导入什么jar包,并未对jar进行系统的归结和管理。而springboot改变了这一现状,他相当于对maven依赖上进行更为全面的归结和管理。

      官网中也说明了springboot依赖管理:

      Each release of Spring Boot provides a curated list of dependencies that it supports. In practice, you do not need to provide a version for any of these dependencies in your build configuration, as Spring Boot manages that for you. When you upgrade Spring Boot itself, these dependencies are upgraded as well in a consistent way,The curated list contains all the spring modules that you can use with Spring Boot as well as a refined list of third party libraries.

      上述解释是:每一个SpringBoot版本都提供了一个它所支持的依赖关系的管理列表。实际上,您不需要在构建配置中为这些依赖项中的任何一个提供版本,因为Spring引导会为您管理这个版本。当您升级Spring引导本身时,这些依赖项也会以一致的方式升级,管理列表包含所有可以与Spring引导一起使用的Spring模块,以及第三方库的优化列表。

二、springboot如何实现依赖管理的(maven方式)?

     当用maven来作项目依赖管理的时候,我们可以在pom.xml文件中看到如下配置:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

       它引入spring-boot-starter-parent父项目,咱们可以继续看spring-boot-starter-parent项目有哪些依赖,我们可以在idea工具中,使用ctrl可以直接进入到项目依赖pom文件中(点击spring-boot-starter-parent):

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath>../../spring-boot-dependencies</relativePath>
</parent>

       它是继续依赖spring-boot-dependencies这个项目,同样的方式进入到spring-boot-dependencies项目POM文件中,主要看properties属性和dependencyManagement属性:

<properties>
<activemq.version>5.15.9</activemq.version>
<antlr2.version>2.7.7</antlr2.version>
<appengine-sdk.version>1.9.73</appengine-sdk.version>
<artemis.version>2.6.4</artemis.version>
<aspectj.version>1.9.2</aspectj.version>
<assertj.version>3.11.1</assertj.version>
<atomikos.version>4.0.6</atomikos.version>
<bitronix.version>2.1.4</bitronix.version>



<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<version>2.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<version>2.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test-autoconfigure</artifactId>
<version>2.1.4.RELEASE</version>
</dependency>

因为内容较多我只列举其中的一部分内容

      从POM文件中可以看到spring-boot-dependencies项目的POM文件才是真正管理springboot项目依赖的。他相当于jar包的依赖仲裁,他提供了jar包的版本管理,以及spring框架和其他第三方组件jar包的依赖管理。

     使用这种依赖管理包括两种方式:

     1.(默认方式)继承Spring Boot的提供的父工程,需要在pom.xml中配置,如下:   

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

      2. 通过scope=import的方式引入, 在很多时候我们需要继承自有的父工程或由于其他设置无法使用Spring Boot提供的父工程。此时可以通过scope=import的方式进行引入,如下:

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.1.4.BUILD-SNAPSHOT</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

      此处通过scope=import的方式导入了依赖的管理配置。但此时我们无法通过在properties中覆盖对应的属性来完成version的控制(因为没有继承父工程)。以此应对的方式是通过在dependencyManagement中进行配置,并且要求在spring-boot-dependencies之前添加即可。同时,对应spring-boot-maven-plugin插件也需要显式配置才可以。

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

三、springboot的场景启动器——Starters

       Starters是一组方便的依赖性描述符,可以包括在应用程序中。您可以为所有Spring和您需要的相关技术组件提供一站式服务,比如“spring-boot-starter-web”,它帮我们导入web模块正常运行所依赖的组件jar包。springboot提供了多种场景启动器,我们可以参照文档查看各种启动器如何导入和使用,以及相关的jar依赖,相关链接。所有的启动器都遵循类似的命名模式;SpringBootStarter-*,其中*是特定类型的应用程序。

SpringBoot 2.X课程学习 | 第二篇:让依赖管理更加便捷(Dependency Management)-LMLPHP

  

06-09 09:53