就像引入其他库一样,我们可以在 Maven 工程中加入 SpringBoot 依赖。然而,最好是从 spring-boot-starter-parent 项目中继承以及声明依赖到 Spring Boot starters。这样做可以 使我们的项目可以重用 SpringBoot 的默认配置。

继承 spring-boot-starter-parent 项目依赖很简单 – 我们只需要在 pom.xml 中定义一个 parent 节点:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.1.RELEASE</version>
</parent>

我们可以在 Maven central 中找到 spring-boot-starter-parent 的最新版本。

使用 starter 父项目依赖很方便,但并非总是可行。例如,如果我们公司都要求项目继承标准 POM,我们就不能依赖 SpringBoot starter 了。

这种情况,我们可以通过对 POM 元素的依赖管理来处理:

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-dependencies</artifactId>
      <version>2.1.1.RELEASE</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>