
2.1.2 Starter、JAR与依赖
为了方便添加依赖,Spring Boot提供了一个概念,称作Starter。Starter是对一个开发场景用到的所有依赖包的集中描述。当我们开发需要某一特定场景时,只需引入对应的Starter即可。
Starter的本质就是JAR包,对Starter的引用与其他JAR包的引用没有不同。在第1章中,我们曾用到过Starter引入Spring Web依赖,它的artifactId是spring-boot-starter-web,其实就是Spring Boot官方提供的一个Starter,对应的引用就是在项目pom.xml中添加dependency,代码如下:

在IDEA中,可以通过Ctrl+鼠标左键单击spring-boot-starter-web来打开这个Starter的POM文件,其文件名为spring-boot-starter-web-2.4.4.pom。在这个POM文件中,可以看到其中最重要的dependencies部分,代码如下:

通过Maven的依赖传递机制,在dependencies中定义的这5个dependency都将在项目中生效。
说明
关于Starter的命名,在第1章中做过详细说明,这里再复习一下。Spring Boot官方Starter以spring-boot-starter开头,第三方Starter以项目名称开头、以-spring-boot-starter结尾。
表2.2~表2.4是官方提供的Starter,了解官方提供的场景有助于我们在开发时做选择。
表2.2中介绍了对技术、依赖封装的Starter。
表2.2 应用类Starter

(续表)

表2.3介绍提供生产环境工具的Starter。
表2.3 生产环境工具类Starter

表2.4介绍Web容器相关Starter。
表2.4 容器类Starter

在网络上可以找到社区提供的Starters。Spring Boot项目的spring-boot-starters模块下有名为README的文件,在这个文件中有对社区提供的Starter说明。如果有必要,我们可以自己编写Starter提交到GitHub上。README文档可以在源代码中找到。