一、问题引入

我们在SSM中使用SpringMVC的时候,需要由我们自己写SpringMVC的配置文件,需要用到什么就要自己配什么,配置起来也特别的麻烦。我们使用SpringBoot的时候没有进行配置,直接就能进行使用,这是为什么呢?

这是因为SpringBoot为我们自动配置好了SpringMVC

1)、我们首先参照官网来看一下关于SpringMVC的自动配置

​ https://docs.spring.io/spring-boot/docs/2.2.1.RELEASE/reference/htmlsingle/#boot-features-developing-web-applications

Spring MVC Auto-configuration

Spring Boot provides auto-configuration for Spring MVC that works well with most applications.

The auto-configuration adds the following features on top of Spring’s defaults:

  • Inclusion of ContentNegotiatingViewResolver and BeanNameViewResolver beans.

    • 自动配置了ViewResolver(视图解析器:根据方法的返回值得到视图对象(View),视图对象决定如何渲染(转发?重定向?))
    • ContentNegotiatingViewResolver:组合所有的视图解析器的;
    • ==如何定制:我们可以自己给容器中添加一个视图解析器;自动的将其组合进来
  • 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, and Formatter等

    • **Converter:转换器,**比如也面提交的是一个数字,但是页面的数字是文本类型的,通过转换器,就能转换成Integer类型
    • Formatter :格式化器,2020-1-1===Date;
  • Support for HttpMessageConverters (covered later in this document).

    • HttpMessageConverter:SpringMVC用来转换Http请求和响应的;
    • HttpMessageConverter是从容器中确定;获取所有的HttpMessageConverter;

自己给容器中添加HttpMessageConverter,只需要将自己的组件注册容器中(@Bean,@Component)

定义错误代码生成规则

我们可以配置一个ConfigurableWebBindingInitializer来替换默认的;(添加到容器)

初始化WebDataBinder;

请求数据=====JavaBean;

If you want to keep Spring Boot MVC features and you want to add additional MVC configuration (interceptors, formatters, view controllers, and other features), you can add your own @Configuration class of type WebMvcConfigurer but without @EnableWebMvc. If you wish to provide custom instances of RequestMappingHandlerMapping, RequestMappingHandlerAdapter, or ExceptionHandlerExceptionResolver, you can declare a WebMvcRegistrationsAdapter instance to provide such components.

2、扩展SpringMVC

编写一个配置类(@Configuration),是WebMvcConfigurerAdapter类型;不能标注@EnableWebMvc;

If you want to take complete control of Spring MVC, you can add your own @Configuration annotated with @EnableWebMvc

3.全面接管SpringMVC

为什么@EnableWebMvc自动配置就失效了?

1)@EnableWebMvc的核心

@Import(DelegatingWebMvcConfiguration.class)

public @interface EnableWebMvc {

2)、

@Configuration

public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {

3)、

@Configuration

@ConditionalOnWebApplication

@ConditionalOnClass({ Servlet.class, DispatcherServlet.class,

WebMvcConfigurerAdapter.class })

//容器中没有这个组件的时候,这个自动配置类才生效

@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)

@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)

@AutoConfigureAfter({ DispatcherServletAutoConfiguration.class,

ValidationAutoConfiguration.class })

public class WebMvcAutoConfiguration {

4)、@EnableWebMvc将WebMvcConfigurationSupport组件导入进来;

5)、导入的WebMvcConfigurationSupport只是SpringMVC最基本的功能;

4.原理

关于ContentNegotiatingViewResolver


我们点进这个ContentNegotiatingViewResolver ,在下面有一个resolveViewName方法

那么是怎么样获取候选的视图对象的呢?

我们点进getCandidateViews这个方法

那么这个组合逻辑又是什么呢?

返回到我们这里


看到首先是new了一个ContentNegotiatingViewResolver对象

我们点进去,

我们所需要的ViewResolvers是从哪里拿到的呢?

我们接着往下边看

我们看到,这里有个初始化方法,里边利用BeanFactoryUtils工具,从容器中获取所有的视图解析器,把这些视图解析器就作为要组合的所有视图解析器。

那么如何定制我们自己的视图解析器呢,通过上面的讲解,就是:我们只需要自己给容器中添加一个视图解析器;然后ContentNegotiatingViewResolver就会将其

组合进来。

我们下面举个简单的例子给大家演示一下

为了方便,我们就在在SpringBoot的启动类中创建一个ViewResolver并将其添加到容器中

那么我们怎么判断是否其作用了呢,我们知道,视图解析器都会被DisPatcherServlet所扫描到,所有的请求都会被DisPatcherServlet类中的doDispatch方法所拦截

在doDispatch方法上打一个断点,debug运行,随便访问一个url,就能看到结果如下。

结果我们发现,我们自己加入的ViewResovler确实生效了

二.总结

1.假如我们需要扩展SpringMVC,只需要编写一个配置类(@Configuration),是WebMvcConfigurerAdapter类型;不标注@EnableWebMvc即可,这个时候我们既保留了所有的自动配置,也能用我们扩展的配置;

2.假如是需要全面接管,即所有的配置都需要我们自己来定义,我们就加上@EnableWebMvc注解即可。

 

SpringBoot中SpringMVC的自动配置以及扩展的更多相关文章

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

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

  2. Springboot中SpringMvc拦截器配置与应用(实战)

    一.什么是拦截器,及其作用 拦截器(Interceptor): 用于在某个方法被访问之前进行拦截,然后在方法执行之前或之后加入某些操作,其实就是AOP的一种实现策略.它通过动态拦截Action调用的对 ...

  3. SpringBoot中对SpringMVC的自动配置

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

  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日记——SpringMvc自动配置与扩展篇

    为了让SpringBoot保持对SpringMVC的全面支持和扩展,而且还要维持SpringBoot不写xml配置的优势,我们需要添加一些简单的配置类即可实现: 通常我们使用的最多的注解是: @Bea ...

  7. SpringBoot | 4.1 SpringMVC的自动配置

    目录 前言 1. SpringMVC框架的设计与流程 1.1 SpringMVC框架的示意图 1.2 SpringMVC的组件流程 2. *自动配置的源码分析 2.1 导入Web场景启动器 2.2 找 ...

  8. springmvc以及springboot中的拦截器配置

    拦截器两种实现   如果不同的controller中都需要拦截器,不能使用相同的拦截器,因为拦截器不能跨controller,这个时候只能为不同的controller配置不同的拦截器,每一个拦截器只能 ...

  9. SpringBoot:配置文件及自动配置原理

    西部开源-秦疆老师:基于SpringBoot 2.1.6 的博客教程 秦老师交流Q群号: 664386224 未授权禁止转载!编辑不易 , 转发请注明出处!防君子不防小人,共勉! SpringBoot ...

随机推荐

  1. Android Studio(十一):代码混淆及打包apk

    Android Studio相关博客: Android Studio(一):介绍.安装.配置 Android Studio(二):快捷键设置.插件安装 Android Studio(三):设置Andr ...

  2. C++第5次作业

    运算符重载 定义 - 运算符重载是对已有的运算符赋予多重含义,使同一个运算符作用于不同类型的数据时导致不同行为. 运算符重载规则 - C++运算符除了少数几个,全都可以重载,且只能重载C++已有的运算 ...

  3. Java使用RandomAccessFile读写文件

    目录 转载自:http://blog.csdn.net/akon_vm/article/details/7429245 Java RandomAccessFile RandomAccessFile是用 ...

  4. Java如何计算hashcode值

    在设计一个类的时候,很可能需要重写类的hashCode()方法,此外,在集合HashSet的使用上,我们也需要重写hashCode方法来判断集合元素是否相等. 下面给出重写hashCode()方法的基 ...

  5. [转]来自后端的逆袭 blazor简介 全栈的福音

    背景 什么是SPA 什么是MPA MPA (Multi-page Application) 多页面应用指的就是最传统的 HTML 网页设计,早期的网站都是这样的设计,所之称为「网页设计」.使用 MPA ...

  6. js随即数字random实现div点击更换背景色

    需求:点击按钮随机给盒子换背景色 用到的知识点:Math.random    Math.round 文章地址 https://www.cnblogs.com/sandraryan/ <!DOCT ...

  7. Python--day47--mysql索引类型介绍

    组合索引(联合索引)详讲:组合索引相对索引合并的缺点是 覆盖索引和索引合并不是真实的索引,只是名词: 命中索引,要避免使用哪些:id.nid是主键 email,num()是索引 1,避免使用like ...

  8. [转]C#中 ??、 ?、 ?: 、?.、?[ ] 问号

    1. 可空类型修饰符(?) 引用类型可以使用空引用表示一个不存在的值,而值类型通常不能表示为空.例如:string str=null; 是正确的,int i=null; 编译器就会报错.为了使值类型也 ...

  9. 2018-8-10-WPF-好看的矢量图标

    title author date CreateTime categories WPF 好看的矢量图标 lindexi 2018-08-10 19:16:53 +0800 2018-5-16 11:4 ...

  10. BZOJ 4236 "JOIOJI"(前缀和+map+pair)

    传送门: [1]:BZOJ [2]:洛谷 •题解 定义数组 a,b,c 分别表示 'J' , 'O' , 'I' 的前缀和: 要想使区间 (L,R] 满足条件当且仅当 a[R]-a[L] = b[R] ...