11.2 添加 Classpath 依赖
Spring Boot provides a number of “Starters” that let you add jars to your classpath. Our sample application has already used spring-boot-starter-parent
in the parent
section of the POM. The spring-boot-starter-parent
is a special starter that provides useful Maven defaults. It also provides a dependency-management
section so that you can omit version
tags for “blessed” dependencies.
SpringBoot提供一许多“Starter”,让您添加jar包到类路径(classpath)中。 我们例子应用程序在POM文件的parent
节点中使用(继承)了spring-boot-starter-parent
。 spring-boot-starter-parent
是一个特殊的“starter”, 提供了有用的Maven默认值。 同时提供了dependency-management
,因此您可以省略“blessed”依赖包的SpringBoot提供一许多“Starter”,让您添加jar包到类路径(classpath)中。 我们例子应用程序中Pom文件(继承的)父POM文件中使用了spring-boot-starter-parent
。 spring-boot-starter-parent
是一个特殊的“starter”, 提供了有用的Maven默认值。 同时还提供了dependency-management
节点,因此您可以(在引用依赖
包时)省略“blessed”依赖的标签。
Other “Starters” provide dependencies that you are likely to need when developing a specific type of application. Since we are developing a web application, we add a spring-boot-starter-web
dependency. Before that, we can look at what we currently have by running the following command:
其他“Starter”提供了您可能在开发一个特定类型的应用程序时需要的依赖。 由于我们要开发一个Web应用程序,因此我们添加了spring-boot-starter-web
依赖。 在此之前, 我们能看一下当前有的依赖,通过运行以下命令:
$ mvn dependency:tree
[INFO] com.example:myproject:jar:0.0.1-SNAPSHOT
The mvn dependency:tree
command prints a tree representation of your project dependencies. You can see that spring-boot-starter-parent
provides no dependencies by itself. To add the necessary dependencies, edit your pom.xml
and add the spring-boot-starter-web
dependency immediately below the parent
section:
mvn dependency:tree
命令打印了您的以树形表现的项目依赖。您能够看见spring-boot-starter-parent
本身没有提供依赖。为添加必需的依赖, 编辑pom.xml
,添加mvn dependency:tree
命令打印了您的以树形表现的项目依赖。您能够看见spring-boot-starter-parent
本身没有提供依赖。为添加必需的依赖, 编辑pom.xml
,在紧接着parent
节点后面添加spring-boot-starter-web
依赖:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
If you run mvn dependency:tree
again, you see that there are now a number of additional dependencies, including the Tomcat web server and Spring Boot itself.
如果您再次运行mvn dependency:tree
, 您会看到现在有许多额外的依赖项, 包括Tomcat Web服务器和SpringBoot本身。
更多建议: