11.2 Adding Classpath Dependencies
Spring Boot提供了大量的”Starter”以便于你增加Jar包到你的类路经下. 我们的应用已在POM文件的parent
节点下配置spring-boot-starter-parent
.
spring-boot-starter-parent
是一个特殊的起步依赖,它提供了实用的Maven默认配置. 当然它还提供dependency-management
节点,能让你在配置依赖时省略版本version
配置.
当你开发一个特定类型的应用时,其他的”Starter”能为你提供需要的相关依赖.因为我们正在开发Web项目,我们增加spring-boot-starter-web
依赖.在增加这个依赖之前,通过输入以下命令来查看我们当前存在的依赖:
$ mvn dependency:tree
[INFO] com.example:myproject:jar:0.0.1-SNAPSHOT
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>
这时你再次运行mvn dependency:tree
命令,你将会看到若干额外的依赖,包括Tomcat服务器和Spring Boot本省.