前言

SpringMVC定位于一个较为松散的组合,展示给用户的视图(View)、控制器返回的数据模型(Model)、定位视图的视图解析器(ViewResolver)和处理适配器(HandlerAdapter)等容器都是独立的。换句话说,通过SpringMVC很容易把后台的数据转换为各种类型的数据,以满足移动互联网数据多样化的要求。

本篇仅为简单介绍SpringMVC的大致组件与流程,详细过程将在后续篇章一一道来。


1. SpringMVC框架的设计与流程

流程和组件是SpringMVC的核心,SpringMVC的流程是围绕DispatcherServlet而工作的。

1.1 SpringMVC框架的示意图

1.2 SpringMVC的组件流程



大致流程是:首先是定义请求分发,让SpringMVC能够产生HandlerMapping;其次是接收请求获取参数;再次是处理业务逻辑获取数据模型ModelAndView;最后是绑定视图和数据模型。

以上组件将会在后续文章讲解,这里仅做一个大概介绍。

组件名称 组件说明
DispatcherServlet 核心组件,前端控制器;
LocalResolver 国际化解析器;
ThemeResolver 主体解析器;
HandlerMapping 处理器映射;
HandlerAdapter 处理器适配器;
HandlerExceptionResolver 处理器异常解析器;
RequestToViewNameTranslator 策略视图名称转换器;
ViewResolver 视图解析器;
FalshMapManager 不常用,FlashMap管理;

以上组件会在SpringMVC初始化时构建出来。

2. *自动配置的源码分析

SpringMVC的自动配置流程是类似第三章了数据库组件自动配置相关内容。

2.1 导入Web场景启动器

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

2.2 找到DispatcherServlet的属性文件

前面提到SpringMVC的核心是DispatcherServlet前端控制器,因此我们找到它的属性文件DispatcherServlet.properties



它定义的对象在SpringMVC开始时就初始化,并且注册进Spring IoC容器中。此外,在这个jar包内定义了很多SpringMVC相关的组件。

3. 自动配置的官网描述

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.

    • 内容协商视图解析器和BeanName视图解析器;
  • Support for serving static resources, including support for WebJars (covered later in this document)).

    • 静态资源(包括webjars);
  • Automatic registration of Converter, GenericConverter, and Formatter beans.

    • 自动注册 Converter,GenericConverter,Formatter;
  • Support for HttpMessageConverters (covered later in this document).

    • 支持 HttpMessageConverters(后续文章有内容协商原理分析);
  • Automatic registration of MessageCodesResolver (covered later in this document).

    • 自动注册 MessageCodesResolver (国际化用,少用,一般直接开发两套页面);
  • Static index.html support.

    • 静态index.html 页支持;
  • Custom Favicon support (covered later in this document).

    • 自定义Favicon;
  • Automatic use of a ConfigurableWebBindingInitializer bean (covered later in this document).

    • 自动使用 ConfigurableWebBindingInitializer,(DataBinder负责将请求数据绑定到JavaBean上);

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 type WebMvcConfigurer but without @EnableWebMvc.

不用@EnableWebMvc注解。使用@Configuration+WebMvcConfigurer自定义规则

If you want to provide custom instances of RequestMappingHandlerMapping, RequestMappingHandlerAdapter, or ExceptionHandlerExceptionResolver, and still keep the Spring Boot MVC customizations, you can declare a bean of type WebMvcRegistrations 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-annotated DelegatingWebMvcConfiguration as described in the Javadoc of @EnableWebMvc.

使用@EnableWebMvc+@Configuration+DelegatingWebMvcConfiguration 全面接管SpringMVC

4. 定制SpringMVC的初始化

Spring提供WebMvcConfigurer接口;对应SpringBoot提供WebMvcAutoConfiguration接口。

4.1 WebMvcConfigurer与WebMvcAutoConfiguration的关系图



在SpringBoot中,自定义通过配置类WebMvcAutoConfiguration定义的,它有一个静态的内部类WebMVCAutoConfigurationAdapter,通过它SpringBoot就自动配置了SpringMVC的初始化。

4.2 SpringMVC可配置项

WebMVCAutoConfigurationAdapter类中,它会读入Spring配置SpringMVC的属此来初始化对应组件,这样便能够在一定程度上实现自定义。可配置项如下:

除此之外,还可以实现WebMvcConfigurer接口加入自己定义的方法。


最后

新人制作,如有错误,欢迎指出,感激不尽!
欢迎关注公众号,会分享一些更日常的东西!
如需转载,请标注出处!

SpringBoot | 4.1 SpringMVC的自动配置的更多相关文章

  1. SpringBoot中对SpringMVC的自动配置

    https://docs.spring.io/spring-boot/docs/1.5.10.RELEASE/reference/htmlsingle/#boot-features-developin ...

  2. java框架之SpringBoot(5)-SpringMVC的自动配置

    本篇文章内容详细可参考官方文档第 29 节. SpringMVC介绍 SpringBoot 非常适合 Web 应用程序开发.可以使用嵌入式 Tomcat,Jetty,Undertow 或 Netty ...

  3. SpringBoot中SpringMVC的自动配置以及扩展

    一.问题引入 我们在SSM中使用SpringMVC的时候,需要由我们自己写SpringMVC的配置文件,需要用到什么就要自己配什么,配置起来也特别的麻烦.我们使用SpringBoot的时候没有进行配置 ...

  4. 7、springmvc的自动配置

    1.springmvc的自动配置 文档:https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/#boot ...

  5. SpringMVC的自动配置解析

    https://docs.spring.io/spring-boot/docs/1.5.10.RELEASE/reference/htmlsingle/#boot-features-developin ...

  6. SpringBoot入门(四)——自动配置

    本文来自网易云社区 SpringBoot之所以能够快速构建项目,得益于它的2个新特性,一个是起步依赖前面已经介绍过,另外一个则是自动配置.起步依赖用于降低项目依赖的复杂度,自动配置负责减少人工配置的工 ...

  7. 0011SpringBoot的@EnableWebMvc全面接管SpringMVC的自动配置(源码)

    所谓的@EnableWebMvc全面接管SpringMVC的自动配置,是指@EnableWebMvc注解会使SpringMVC的自动配置失效,原理如下: 1.查看@EnableWebMvc的源码,如下 ...

  8. 接管SpringBoot对Activiti的数据源自动配置

    SpringBoot的自动配置真的让人又爱又恨,但还是爱更多一点. SpringBoot想要帮我们自动配置好一切,但是有时候配置的却并不是我们需要的,甚至有时候会默默的坑我们. 我的项目是一个多数据源 ...

  9. springboot web项目创建及自动配置分析(thymeleaf+flyway)

    @ 目录 源码分析 webjars thymeleaf thymeleaf语法 springmvc 启动配置原理 集成flyway插件 springboot 创建web项目只需要引入对应的web-st ...

随机推荐

  1. 2018-10-14普及模拟赛」Hash 键值 (hash)

    今天,带大家看一看一道思维题... Hash 键值 (hash) 题目描述 Marser沉迷hash无法自拔,然而他发现自己记不住hash键值了-- Marser使用的hash函数是一个单纯的取模运算 ...

  2. hbuilder 开发app 自动升级

    使用huilder 开发app  ,实现app升级功能 1. var wgtVer = null; //用于获取系统当前版本 var currentversion = null; //用于获取系统最新 ...

  3. 『心善渊』Selenium3.0基础 — 19、使用Selenium操作文件的上传和下载

    目录 1.Selenium实现文件上传 (1)页面中的文件上传说明 (2)文件上传示例 (3)总结 2.Selenium实现文件下载 (1)Firefox浏览器文件下载 1)操作步骤: 2)文件下载示 ...

  4. 在 Docker 上配置 Oracle

    地址:https://github.com/wnameless/docker-oracle-xe-11g .直接 git clone 到本地就行了 ##安装 docker shell 下: docke ...

  5. 数据库表的自增ID createDate和updateDate 用JPA注解代替触发器实现

    对于数据库表的自增ID , createDate和updateDate 等字段,用JPA注解代替触发器实现,效率会高很多. 由于这些属性很多entity都有 可以写成两个基本entity :BaseE ...

  6. 重新梳理调度器——GMP 调度模型

    调度器--GMP 调度模型 Goroutine 调度器,它是负责在工作线程上分发准备运行的 goroutines. 首先在讲 GMP 调度模型之前,我们先了解为什么会有这个模型,之前的调度模型是什么样 ...

  7. CURL 实战下载

    #include <string> #include <stdio.h> #include <iostream> #include<fstream> # ...

  8. 「SPOJ 3105」Power Modulo Inverted

    「SPOJ 3105」Power Modulo Inverted 传送门 题目大意: 求关于 \(x\) 的方程 \[a^x \equiv b \;(\mathrm{mod}\; p) \] 的最小自 ...

  9. Linux系统引导过程及排除启动故障

    一.Linux操作系统引导过程二.系统初始化进程1.init进程2.Systemd3.Systemd单元类型三.排除启动类故障[1].修复MBR扇区故障(含实验过程)[2].修复GRUB引导故障●方法 ...

  10. Spring Boot(三):Spring Boot中的事件的使用 与Spring Boot启动流程(Event 事件 和 Listeners监听器)

    前言:在讲述内容之前 希望大家对设计模式有所了解 即使你学会了本片的内容 也不知道什么时候去使用 或者为什么要这样去用 观察者模式: 观察者模式是一种对象行为模式.它定义对象间的一种一对多的依赖关系, ...