Spring Boot不需要像传统Spring MVC项目一样需要一些很复杂的配置,那你们有没有想过到底为什么是这样的呢?
下面以最常用的Spring MVC为例进行说明。首先打开Maven的本地仓库,找到对应Spring Boot的文件夹,可以看到如下图所示的目录。
这里先谈spring-boot-start-web的内容,未来还会谈到spring-boot-autoconfigure文件夹的内容,所以图 2-10 中一并加了框。打开spring-boot-start-web文件夹(我的在C:\Users\Administrator.m2\repository\org\springframework\boot\spring-boot-starter-web),就可以看到一个名为spring-boot- starter-web-2.0.0.RELEASE.pom的文件,打开它就可以看到如下代码。
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<!-- This module was also published with a richer model, Gradle metadata, -->
<!-- which should be used instead. Do not delete the following line which -->
<!-- is to indicate to Gradle or any Gradle module metadata file consumer -->
<!-- that they should prefer consuming it instead. -->
<!-- do_not_remove: published-with-gradle-metadata -->
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.4.4</version>
<name>spring-boot-starter-web</name>
<description>Starter for building web, including RESTful, applications using Spring MVC. Uses Tomcat as the default embedded container</description>
<url>https://spring.io/projects/spring-boot</url>
<organization>
<name>Pivotal Software, Inc.</name>
<url>https://spring.io</url>
</organization>
<licenses>
<license>
<name>Apache License, Version 2.0</name>
<url>https://www.apache.org/licenses/LICENSE-2.0</url>
</license>
</licenses>
<developers>
<developer>
<name>Pivotal</name>
<email>info@pivotal.io</email>
<organization>Pivotal Software, Inc.</organization>
<organizationUrl>https://www.spring.io</organizationUrl>
</developer>
</developers>
<scm>
<connection>scm:git:git://github.com/spring-projects/spring-boot.git</connection>
<developerConnection>scm:git:ssh://git@github.com/spring-projects/spring-boot.git</developerConnection>
<url>https://github.com/spring-projects/spring-boot</url>
</scm>
<issueManagement>
<system>GitHub</system>
<url>https://github.com/spring-projects/spring-boot/issues</url>
</issueManagement>
<dependencies>
<dependency>
<!-- Spring Boot 依赖-->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.4.4</version>
<scope>compile</scope>
</dependency>
<!-- JSON 依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
<version>2.4.4</version>
<scope>compile</scope>
</dependency>
<!-- Tomcat 依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>2.4.4</version>
<scope>compile</scope>
</dependency>
<!-- Spring Web 依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.3.5</version>
<scope>compile</scope>
</dependency>
<!-- Spring Web MVC 依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.5</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
从这里可以看出,当加入spring-boot-starter-web后,它会通过Maven将对应的资源加载到我们的工程中,这样便能够形成依赖。但是这样还不足以运行Spring MVC项目,要运行它还需要对Spring MVC进行配置,让它能够生产Spring MVC所需的对象,才能启用Spring MVC,所以还需要进一步探讨。
为了探讨Spring MVC在Spring Boot自动配置的问题,首先在本地下载的Maven仓库的目录spring-boot-autoconfigure中找到spring-boot-autoconfigure-2.0.0.RELEASE-sources.jar的包。它是一个源码包,把它解压缩出来,打开它目录下的子目录org\springframework\boot\autoconfigure\web\servlet后,我们就可以看到许多配置类,如下图所示。
这里可以看到存在很多的类,其中加框的类DispatcherServletAutoConfiguration就是一个对DispatcherServlet进行自动配置的类。