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

1、前言 

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

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

      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.spring

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

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

     当用maven来做项目依赖管理的时候,咱们能够在pom.xml文件中看到以下配置:app

<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属性:maven

<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包的依赖管理。ide

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

     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>

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

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

相关文章
相关标签/搜索