Spring Boot 2.4 第一个示例程序添加 Classpath 依赖

427 阅读2分钟

Spring Boot 提供了一系列的 “Starters” 来让你将 Jar 添加到你的 classpath 路径中。 我们的项目提供了一系列的 smoke tests,需要使用 spring-boot-starter-parent 定义在 POM 文件的 parent 部分。如果你对 smoke tests 不太了解的话,请参考相关定义和搜索相关文档。 在这里 smoke tests 的定义就是能够让项目能够跑起来,并且实现一些基本的功能。

spring-boot-starter-parent 是一个特殊的 starter。在这个定义中,我们为 Maven 提供了一些有用的默认配置。 同时 spring-boot-starter-parent 还提供了一些 [dependency-management]。 因为上面的的配置,针对依赖,你可以针对 “blessed” 忽略掉 version 标签。

当你针对 Spring Boot 在应用中,想使用一些特定的功能或者构建一些特定的应用,你可以针对需要来选择其他的 “Starters”。 针对当前的示例,我们打算是构建一个 Web 应用程序,因此我们需要添加一个 spring-boot-starter-web 到依赖(dependency)中。 在进行上面的操作之前,我们可以通过运行下面的命令来查看我们当前项目中使用的依赖:

$ mvn dependency:tree

[INFO] com.example:myproject:jar:0.0.1-SNAPSHOT

spring-boot-tree-01

mvn dependency:tree 命令行将会针对你的项目使用的依赖打印出依赖的树结构。 现在,你可以看到 spring-boot-starter-parent,但是还没有添加其他的依赖。 如果想添加一些必要的依赖,编辑你的 pom.xml 然后添加 spring-boot-starter-web 依赖代码到 pom.xml 文件的 parent 部分后面。 具体的代码内容,请参考下面的示例:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

当你再次运行 mvn dependency:tree 的时候,你可以看到在你当前的项目中已经添加上去一些其他的依赖了。包括有 Tomcat web server 和 Spring Boot 本身。

spring-boot-tree-02

上图显示的是添加 Web 依赖后显示的树。我们会发现添加的 jar 已经明显多了很多。

使用 Maven 的好处是,你不再需要指定版本,可以通过 Maven 直接进行管理。

www.ossez.com/t/spring-bo…