11.2、添加 Classpath 依赖

Spring Boot 提供了多个 “Starter”,可以让您方便地将 jar 添加到 classpath 下。我们的示例应用已经在 POM 的 parent 部分使用了 spring-boot-starter-parentspring-boot-starter-parent 是一个特殊 Starter,它提供了有用的 Maven 默认配置。此外它还提供了依赖管理功能,您可以忽略这些依赖的版本(version)标签。

其他 Starter 只提供在开发特定应用时可能需要到的依赖。由于我们正在开发一个 web 应用,因此我们将添加一个 spring-boot-starter-web 依赖 , 但在此之前,让我们来看看目前拥有的。

  1. mvn dependency:tree
  2. [INFO] com.example:myproject:jar:0.0.1-SNAPSHOT

mvn dependency:tree 命令以树的形式打印项目的依赖。您可以看到 spring-boot-starter-parent 本身不提供依赖。我们可以在 parent 下方立即编辑 pom.xml 并添加 spring-boot-starter-web 依赖:

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-web</artifactId>
  5. </dependency>
  6. </dependencies>

如果您再次运行 mvn dependency:tree,将会看到现在有许多附加的依赖,包括了 Tomcat web 服务器和 Spring Boot 本身。