Spring Boot starter入门

1年前 (2024-04-26)

传统的 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网站站点" rel="nofollow" />

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网站站点" rel="nofollow" />