SpringMVC定位于一个较为松散的组合,展示给用户的视图(View)、控制器返回的数据模型(Model)、定位视图的视图解析器(ViewResolver)和处理适配器(HandlerAdapter)等容器都是独立的。换句话说,通过SpringMVC很容易把后台的数据转换为各种类型的数据,以满足移动互联网数据多样化的要求。
本篇仅为简单介绍SpringMVC的大致组件与流程,详细过程将在后续篇章一一道来。
流程和组件是SpringMVC的核心,SpringMVC的流程是围绕DispatcherServlet而工作的。
大致流程是:首先是定义请求分发,让SpringMVC能够产生HandlerMapping
;其次是接收请求获取参数;再次是处理业务逻辑获取数据模型ModelAndView
;最后是绑定视图和数据模型。
以上组件将会在后续文章讲解,这里仅做一个大概介绍。
组件名称 | 组件说明 |
---|---|
DispatcherServlet | 核心组件,前端控制器; |
LocalResolver | 国际化解析器; |
ThemeResolver | 主体解析器; |
HandlerMapping | 处理器映射; |
HandlerAdapter | 处理器适配器; |
HandlerExceptionResolver | 处理器异常解析器; |
RequestToViewNameTranslator | 策略视图名称转换器; |
ViewResolver | 视图解析器; |
FalshMapManager | 不常用,FlashMap管理; |
以上组件会在SpringMVC初始化时构建出来。
SpringMVC的自动配置流程是类似第三章了数据库组件自动配置相关内容。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
前面提到SpringMVC的核心是DispatcherServlet
前端控制器,因此我们找到它的属性文件DispatcherServlet.properties
:
它定义的对象在SpringMVC开始时就初始化,并且注册进Spring IoC容器中。此外,在这个jar包内定义了很多SpringMVC相关的组件。
SpringBoot配置SpringMVC在SpringBoot官网已经说明了,可以参考以下翻译。
官网地址:7.1.1. Spring MVC Auto-configuration
Spring Boot provides auto-configuration for Spring MVC that works well with most applications.(SpringBoot为SpringMVC提供了自动配置,因此大多场景我们都无需自定义配置)
The auto-configuration adds the following features on top of Spring’s defaults:
(自动化配置包括以下默认特性)
Inclusion of ContentNegotiatingViewResolver
and BeanNameViewResolver
beans.
Support for serving static resources, including support for WebJars (covered later in this document)).
Automatic registration of Converter
, GenericConverter
, and Formatter
beans.
Support for HttpMessageConverters
(covered later in this document).
Automatic registration of MessageCodesResolver
(covered later in this document).
Static index.html
support.
Custom Favicon
support (covered later in this document).
Automatic use of a ConfigurableWebBindingInitializer
bean (covered later in this document).
If you want to keep those Spring Boot MVC customizations and make more MVC customizations (interceptors, formatters, view controllers, and other features), you can add your own
@Configuration
class of typeWebMvcConfigurer
but without@EnableWebMvc
.不用@EnableWebMvc注解。使用@Configuration+WebMvcConfigurer自定义规则;
If you want to provide custom instances of
RequestMappingHandlerMapping
,RequestMappingHandlerAdapter
, orExceptionHandlerExceptionResolver
, and still keep the Spring Boot MVC customizations, you can declare a bean of typeWebMvcRegistrations
and use it to provide custom instances of those components.声明WebMvcRegistrations改变默认底层组件;
If you want to take complete control of Spring MVC, you can add your own
@Configuration
annotated with@EnableWebMvc
, or alternatively add your own@Configuration
-annotatedDelegatingWebMvcConfiguration
as described in the Javadoc of@EnableWebMvc
.使用@EnableWebMvc+@Configuration+DelegatingWebMvcConfiguration 全面接管SpringMVC;
Spring提供WebMvcConfigurer接口;对应SpringBoot提供WebMvcAutoConfiguration接口。
在SpringBoot中,自定义通过配置类WebMvcAutoConfiguration
定义的,它有一个静态的内部类WebMVCAutoConfigurationAdapter
,通过它SpringBoot就自动配置了SpringMVC的初始化。
在WebMVCAutoConfigurationAdapter
类中,它会读入Spring配置SpringMVC的属此来初始化对应组件,这样便能够在一定程度上实现自定义。可配置项如下:
除此之外,还可以实现WebMvcConfigurer
接口加入自己定义的方法。
SpringBoot | 4.1 SpringMVC的自动配置
原文:https://www.cnblogs.com/dlhjw/p/15144219.html