传统的 Spring 项目想要运行,不仅需要导入各种依赖,还要对各种 XML 配置文件进行配置,十分繁琐,但 Spring Boot 项目在创建完成后,即使不编写任何代码,不进行任何配置也能够直接运行,这都要归功于 Spring Boot 的 starter 机制。本节我们将对 stater 进行介绍。

starter

Spring Boot 将日常企业应用研发中的各种场景都抽取出来,做成一个个的 starter(启动器),starter 中整合了该场景下各种可能用到的依赖,用户只需要在 Maven 中引入 starter 依赖,SpringBoot 就能自动扫描到要加载的信息并启动相应的默认配置。starter 提供了大量的自动配置,让用户摆脱了处理各种依赖和配置的困扰。所有这些 starter 都遵循着约定成俗的默认配置,并允许用户调整这些配置,即遵循“约定大于配置”的原则。

并不是所有的 starter 都是由 Spring Boot 官方提供的,也有部分 starter 是第三方技术厂商提供的,例如 druid-spring-boot-starter 和 mybatis-spring-boot-starter 等等。当然也存在个别第三方技术,Spring Boot 官方没提供 starter,第三方技术厂商也没有提供 starter。

以 spring-boot-starter-web 为例,它能够为提供 Web 开发场景所需要的几乎所有依赖,因此在使用 Spring Boot 开发 Web 项目时,只需要引入该 Starter 即可,而不需要额外导入 Web 服务器和其他的 Web 依赖。

在 pom.xml 中引入 spring-boot-starter-web,示例代码如下。

<?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> <!--SpringBoot父项目依赖管理-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.5</version>
<relativePath/>
</parent>
....
<dependencies>
<!--导入 spring-boot-starter-web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
...
</dependencies>
...
</project>

在该项目中执行以下 mvn 命令查看器依赖树。

  • mvn dependency:tree

执行结果如下。

[INFO] Scanning for projects...
[INFO]
[INFO] --------------------< net.biancheng.www:helloworld >--------------------
[INFO] Building helloworld 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:3.1.2:tree (default-cli) @ helloworld ---
[INFO] net.biancheng.www:helloworld:jar:0.0.1-SNAPSHOT
[INFO] \- org.springframework.boot:spring-boot-starter-web:jar:2.4.5:compile
[INFO] +- org.springframework.boot:spring-boot-starter:jar:2.4.5:compile
[INFO] | +- org.springframework.boot:spring-boot:jar:2.4.5:compile
[INFO] | +- org.springframework.boot:spring-boot-autoconfigure:jar:2.4.5:compile
[INFO] | +- org.springframework.boot:spring-boot-starter-logging:jar:2.4.5:compile
[INFO] | | +- ch.qos.logback:logback-classic:jar:1.2.3:compile
[INFO] | | | +- ch.qos.logback:logback-core:jar:1.2.3:compile
[INFO] | | | \- org.slf4j:slf4j-api:jar:1.7.30:compile
[INFO] | | +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.13.3:compile
[INFO] | | | \- org.apache.logging.log4j:log4j-api:jar:2.13.3:compile
[INFO] | | \- org.slf4j:jul-to-slf4j:jar:1.7.30:compile
[INFO] | +- jakarta.annotation:jakarta.annotation-api:jar:1.3.5:compile
[INFO] | +- org.springframework:spring-core:jar:5.3.6:compile
[INFO] | | \- org.springframework:spring-jcl:jar:5.3.6:compile
[INFO] | \- org.yaml:snakeyaml:jar:1.27:compile
[INFO] +- org.springframework.boot:spring-boot-starter-json:jar:2.4.5:compile
[INFO] | +- com.fasterxml.jackson.core:jackson-databind:jar:2.11.4:compile
[INFO] | | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.11.4:compile
[INFO] | | \- com.fasterxml.jackson.core:jackson-core:jar:2.11.4:compile
[INFO] | +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.11.4:compile
[INFO] | +- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.11.4:compile
[INFO] | \- com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.11.4:compile
[INFO] +- org.springframework.boot:spring-boot-starter-tomcat:jar:2.4.5:compile
[INFO] | +- org.apache.tomcat.embed:tomcat-embed-core:jar:9.0.45:compile
[INFO] | +- org.glassfish:jakarta.el:jar:3.0.3:compile
[INFO] | \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:9.0.45:compile
[INFO] +- org.springframework:spring-web:jar:5.3.6:compile
[INFO] | \- org.springframework:spring-beans:jar:5.3.6:compile
[INFO] \- org.springframework:spring-webmvc:jar:5.3.6:compile
[INFO] +- org.springframework:spring-aop:jar:5.3.6:compile
[INFO] +- org.springframework:spring-context:jar:5.3.6:compile
[INFO] \- org.springframework:spring-expression:jar:5.3.6:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.505 s
[INFO] Finished at: 2021-04-30T14:52:30+08:00
[INFO] ------------------------------------------------------------------------

从以上结果中,我们可以看到 Spring Boot 导入了 springframework、logging、jackson 以及 Tomcat 等依赖,而这些正是我们在开发 Web 项目时所需要的。

您可能会发现一个问题,即在以上 pom.xml 的配置中,引入依赖 spring-boot-starter-web 时,并没有指明其版本(version),但在依赖树中,我们却看到所有的依赖都具有版本信息,那么这些版本信息是在哪里控制的呢?

其实,这些版本信息是由 spring-boot-starter-parent(版本仲裁中心) 统一控制的。

spring-boot-starter-parent

spring-boot-starter-parent 是所有 Spring Boot 项目的父级依赖,它被称为 Spring Boot 的版本仲裁中心,可以对项目内的部分常用依赖进行统一管理。

<!--SpringBoot父项目依赖管理-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.5</version>
<relativePath/>
</parent>

Spring Boot 项目可以通过继承 spring-boot-starter-parent 来获得一些合理的默认配置,它主要提供了以下特性:

  • 默认 JDK 版本(Java 8)
  • 默认字符集(UTF-8)
  • 依赖管理功能
  • 资源过滤
  • 默认插件配置
  • 识别 application.properties 和 application.yml 类型的配置文件

查看 spring-boot-starter- parent 的底层代码,可以发现其有一个父级依赖 spring-boot-dependencies。

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.4.5</version>
</parent>
 

spring-boot-dependencies 的底层代码如下。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.4.5</version>
<packaging>pom</packaging>
....
<properties>
<activemq.version>5.16.1</activemq.version>
<antlr2.version>2.7.7</antlr2.version>
<appengine-sdk.version>1.9.88</appengine-sdk.version>
<artemis.version>2.15.0</artemis.version>
<aspectj.version>1.9.6</aspectj.version>
<assertj.version>3.18.1</assertj.version>
<atomikos.version>4.0.6</atomikos.version>
....
</properties> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-amqp</artifactId>
<version>${activemq.version}</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-blueprint</artifactId>
<version>${activemq.version}</version>
</dependency>
...
</dependencies>
</dependencyManagement> <build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>${build-helper-maven-plugin.version}</version>
</plugin>
<plugin>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>${flyway.version}</version>
</plugin>
...
</plugins>
</pluginManagement>
</build>
</project>

以上配置中,部分元素说明如下:

  • dependencyManagement :负责管理依赖;
  • pluginManagement:负责管理插件;
  • properties:负责定义依赖或插件的版本号。

spring-boot-dependencies 通过 dependencyManagement 、pluginManagement 和 properties 等元素对一些常用技术框架的依赖或插件进行了统一版本管理,例如 Activemq、Spring、Tomcat 等。

starter的更多相关文章

  1. Microsoft IoT Starter Kit 开发初体验

    1. 引子 今年6月底,在上海举办的中国国际物联网大会上,微软中国面向中国物联网社区推出了Microsoft IoT Starter Kit ,并且免费开放1000套的申请.申请地址为:http:// ...

  2. Spring Boot(3)---自定义spring boot starter 问题

    1. "Failed to process import candidates for configuration class [com.simple.....]": 主要原因: ...

  3. 各种 starter poms (启动器)

    starter包含了搭建项目,快速运行所需的依赖.它是一个依赖关系描述符的集合.当应用需要一种spring的服务时,不需要粘贴拷贝大量的依赖关系描述符.例如想在spring中使用redis,只需要在项 ...

  4. Delphi 10.1 Berlin Starter Edition

    Delphi 10.1 Berlin Starter Edition Embarcadero® Delphi 10.1 Berlin Starter is a great way to get sta ...

  5. Starter Set of Functional Interfaces

    Java Development Kit 8 has a number of functional interfaces. Here we review the starter set-the int ...

  6. 【AngularJs】---Error: [$injector:modulerr] Failed to instantiate module starter.services

    [遇到问题解决问题,原谅我这个菜鸟] 加了services angular.module('starter', ['ionic', 'starter.controllers', 'starter.se ...

  7. asp.net的3个经典范例(ASP.NET Starter Kit ,Duwamish,NET Pet Shop)学习资料

    asp.net的3个经典范例(ASP.NET Starter Kit ,Duwamish,NET Pet Shop)学习资料 NET Pet Shop .NET Pet Shop是一个电子商务的实例, ...

  8. Spring Boot的启动器Starter详解

    Spring Boot的启动器Starter详解 作者:chszs,未经博主允许不得转载.经许可的转载需注明作者和博客主页:http://blog.csdn.net/chszs Spring Boot ...

  9. Microsoft IoT Starter Kit 开发初体验-反馈控制与数据存储

    在上一篇文章<Microsoft IoT Starter Kit 开发初体验>中,讲述了微软中国发布的Microsoft IoT Starter Kit所包含的硬件介绍.开发环境搭建.硬件 ...

  10. React Starter Kit 中文文档

    最近没事又翻译了个玩意. Github上的一个Star 非常高的 React 样板程序. 由Node.js,Express,GraphQL和React构建,可选加入Redux等,并可以包含Webpac ...

随机推荐

  1. insert语句

    7.4.插入数据insert(DML语句) 语法格式: insert into 表名(字段名1,字段名2,字段名3...) values(值1,值2,值3): 注意:字段名和值要一一对应.什么是一一对 ...

  2. GoLang设计模式20 - 组合模式

    定义 组合模式是一种结构型设计模式. 当我们想把一组对象当做一个整体来处理时就可以考虑使用组合模式. 组合模式被视为是接口型设计模式是因为它将一组对象组装为一个树状结构.这个树状结构中的每个独立组件都 ...

  3. web.xml最新配置

    <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmln ...

  4. python列表和索引--7

    备注:列表元素索引下限从0开始,列表用[ ]表示

  5. Zookeeper 源码解析-环境准备

    为了深入学习Zookeeper,准备把zookeeper源码导入eclipse中学习 一.Ant环境准备 因为我自己用的是Mac,直接输入命令:brew install ant即可 如果是window ...

  6. SpringBoot 简单介绍

    一.springboot是啥? 1.什么是springboot? 1.1springboot是spring生态圈的一个轻量级框架,换句话说springboot就是spring,是spring的一个衍生 ...

  7. FFT通过傅里叶级数图解频域补零时域内插

    在时域频域的信号分析的过程中,一个常见的说法叫:频域数据补零会让时域数据内插. 意思是在频域数据中多补几个零,再做ifft(逆傅里叶变换)后的时域数据,会变得更加"细腻",分辨率会 ...

  8. 一些求和式的估算 & 杜教筛时间复杂度证明

    本文内容概要: \(A=\sum\limits_{i=1}^n\dfrac1{\sqrt i}=1+\dfrac1{\sqrt2}+\cdots+\dfrac1{\sqrt n}\) \(O(\sqr ...

  9. SpringBoot的.gitignore文件使用

    简介 临时文件,编译的中间文件等不要提交到代码仓库,这时就要设置相应的忽略规则,来忽略这些文件的提交.git提供了一个.gitignore,来自动忽略这些配置文件 配置规则 # 表示此为注释,将被Gi ...

  10. JSP两种声明变量的区别

    感谢大佬:https://blog.csdn.net/tiercel2008/article/details/11553899?utm_source=distribute.pc_relevant.no ...