文章目录
1、在Spring Boot中搭建MVC项目
关于Spring MVC的详细学习可以参考系列博客《Spring MVC使用篇系列博客传送门》
1.1 引入Spring MVC相关依赖
在Spring Boot中使用Spring MVC仅需在pom文件中引入spring-boot-starter-web即可。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
在Spring Boot的启动机制下启用Spring MVC,它会在开始初始化一些Spring MVC的重要组件,如DispatcherServlet、HandlerAdapter的实现类RequestMappingHandlerAdapter等组件对象。
关于这些组件的初始化,我们可以在spring-web-xxx.jar包的属性文件DispatcherServlet.properties,它定义的对象都是在Spring MVC开始时就初始化,并且存放在Spring IoC容器中的。其源码如下:
# Default implementation classes for DispatcherServlet's strategy interfaces.
# Used as fallback when no matching beans are found in the DispatcherServlet context.
# Not meant to be customized by application developers.
# 国际化解析器
org.springframework.web.servlet.LocaleResolver=org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver
# 主题解析器
org.springframework.web.servlet.ThemeResolver=org.springframework.web.servlet.theme.FixedThemeResolver
# HandlerMapping实例
org.springframework.web.servlet.HandlerMapping=org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,\
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping,\
org.springframework.web.servlet.function.support.RouterFunctionMapping
# 处理器适配器
org.springframework.web.servlet.HandlerAdapter=org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,\
org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,\
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter,\
org.springframework.web.servlet.function.support.HandlerFunctionAdapter
# 处理器异常解析器
org.springframework.web.servlet.HandlerExceptionResolver=org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver,\
org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver,\
org.springframework.web.servlet.mvc.support