SpringMVC自动配置

1.SpringBoot官方文档对SpringMVC的默认配置:

  • Inclusion of ContentNegotiatingViewResolver and BeanNameViewResolver beans.

    • 自动配置了ViewResolver(视图解析器:根据方法返回值得到视图对象,试图对象决定如何渲染)
    • 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 类型转换器 :String ===> 封装进去User

    • Formatter 格式化器 :2017-12-17 ===> Date

      public void addFormatters(FormatterRegistry registry) {
      ApplicationConversionService.addBeans(registry, this.beanFactory);
      }
      public static void addBeans(FormatterRegistry registry, ListableBeanFactory beanFactory) {
      Set<Object> beans = new LinkedHashSet();
      beans.addAll(beanFactory.getBeansOfType(GenericConverter.class).values());
      beans.addAll(beanFactory.getBeansOfType(Converter.class).values());

      所以springboot同时也将容器中所有的Converter Formatter GenericConverter加入到组件中

      自己添加的格式化转换器,我们只需要放在容器中即可

  • Support for HttpMessageConverters (covered later in this document).

    • HttpMessageConverters : SpringMVC用来转换Http请求和相应的;(User对象转换成Json数据)

    • 只有一个构造器给ObjectProvider赋值 ,所以参数直接从容器中获取, HttpMessageConverters 是从容器中确定,获取所有的HttpMessageConverter

      private final ObjectProvider<HttpMessageConverters> messageConvertersProvider;
      public WebMvcAutoConfigurationAdapter(…… ObjectProvider<HttpMessageConverters> messageConvertersProvider…………) {
      this.messageConvertersProvider = messageConvertersProvider;

      自己添加的HttpMessageConverter,只需要直接放入容器(@Bean、@Component)

  • Automatic registration of MessageCodesResolver (covered later in this document).

    • 定义错误代码的生成规则
  • Static index.html support.

    • 静态首页访问
  • Custom Favicon support (covered later in this document).

    • favicon.ico
  • Automatic use of a ConfigurableWebBindingInitializer bean (covered later in this document).

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

       初始化WebDataBinder
      请求数据===JavaBean 将数据绑定成为我们要封装的对象,用到类型转换器,格式化器等等
      ConfigurableWebBindingInitializer
      public void initBinder(WebDataBinder binder) {
      binder.setAutoGrowNestedPaths(this.autoGrowNestedPaths);
      if (this.directFieldAccess) {
      binder.initDirectFieldAccess();
      }

org.springframework.boot.autoconfigure.web:web的所有自动场景;

2. 扩展SpringMVC

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.

    <mvc:view-controller path="/hello" view-name="success"/>
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/hello"/>
<bean></bean>
</mvc:interceptor>
</mvc:interceptors>

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

@Configuration
//WebMvcConfigurer接口中有很多方法的对应的是以往Springmvc配置文件中的属性
//使用其来扩展SpringMVC的功能
public class MyMvcConfig implements WebMvcConfigurer { @Override
public void addViewControllers(ViewControllerRegistry registry) {
//浏览器发送/jiat请求来到 success
registry.addViewController("/jiat").setViewName("success");
} }

原理:

  1. WebMvcAutoConfiguration是SpringMVC的自动配置类
  2. 在做其他自动配置时会导入;@Import(EnableWebMvcConfiguration.class)
  3. 容器中所有的WebMvcConfiguration都会起作用
  4. 我们的配置类也会被调用

3.全面接管SpringMVC(在我们的WebMvcConfigurer配置类上加上一个@EnableWebMvc)

SpringBoot对SpringMVC的自动配置不需要了,所有都是我们自己配置,所有的SpringMVC模块自动配置全部失效,包括静态资源

@EnableWebMvc能使默认配置生效的原因

  1. 添加了@EnableWebMvc后会为容器导入一个WebMvcConfigurationSupport配置类,添加相应组件到容器中
@Import({DelegatingWebMvcConfiguration.class}) //导入一个DelegatingWebMvcConfiguration
public @interface EnableWebMvc {
//为容器添加一个WebMvcConfigurationSupport
@Configuration(
proxyBeanMethods = false
)
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {
  1. 而 WebMvcAutoConfiguration 的注解上有一个“@ConditionalOnMissingBean({WebMvcConfigurationSupport.class})”,当存在如果容器中存在WebMvcConfigurationSupport对象时,WebMvcAutoConfiguration 自动配置失效
@Configuration(
proxyBeanMethods = false
)
@ConditionalOnWebApplication(
type = Type.SERVLET
)
@ConditionalOnClass({Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class})
//如果容器中存在WebMvcConfigurationSupport对象,则以下所有配置不生效
@ConditionalOnMissingBean({WebMvcConfigurationSupport.class})
@AutoConfigureOrder(-2147483638)
@AutoConfigureAfter({DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class, ValidationAutoConfiguration.class})
public class WebMvcAutoConfiguration {

接管SpringMVC总结

  1. @EnableWebMvc将WebMvcConfigurationSupport组件导入容器中
  2. WebMvcConfigurationSupport只是SpringMVC的最基本功能
  3. 用户可以在实现基本功能的WebMvcConfigurationSupport上定制自己的webmvc

4. 如何修改SpringBoot的默认配置

模式:

  1. SpingBoot在自动配置很多组件的时候,先看容器中有没有用户自己配置的(@Bean、@Component)如果有就用用户配置的,没有才自动配置,如果有些组件可以多个(ViewResolver)将用户配置的和自己默认的组合起来
  2. 在SpringBoot中会有非常多的xxxConfigurer帮助我们进行扩展
  3. 在SpringBoot中会有很多的xxxCustomizer帮助我们进行定制配置

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

  1. spring-boot spring-MVC自动配置

    Spring MVC auto-configuration Spring Boot 自动配置好了SpringMVC 以下是SpringBoot对SpringMVC的默认配置:==(WebMvcAuto ...

  2. SpringBoot源码学习系列之SpringMVC自动配置

    目录 1.ContentNegotiatingViewResolver 2.静态资源 3.自动注册 Converter, GenericConverter, and Formatter beans. ...

  3. SpringBoot扩展SpringMVC自动配置

    SpringBoot中自动配置了 ViewResolver(视图解析器) ContentNegotiatingViewResolver(组合所有的视图解析器) 自动配置了静态资源文件夹.静态首页.fa ...

  4. 【SpringBoot】SpringBoot与SpringMVC自动配置(五)

    本文介绍SpringBoot对Spring MVC自动配置,SpringBoot自动配置原理可以参考:[SpringBoot]SpringBoot配置与单元测试(二) 首先新建一个SpringBoot ...

  5. 9 Web开发——springmvc自动配置原理

    官方文档目录: https://docs.spring.io/spring-boot/docs/2.1.0.RELEASE/reference/htmlsingle/#boot-features-sp ...

  6. springboot mvc自动配置(三)初始化mvc的组件

    所有文章 https://www.cnblogs.com/lay2017/p/11775787.html 正文 在springboot mvc自动配置的时候,获得了DispatcherServlet和 ...

  7. 关于SpringBoot的自动配置和启动过程

    一.简介 Spring Boot简化了Spring应用的开发,采用约定大于配置的思想,去繁从简,很方便就能构建一个独立的.产品级别的应用. 1.传统J2EE开发的缺点 开发笨重.配置繁多复杂.开发效率 ...

  8. 4_2.springboot2.x配置之springmvc自动配置

    1.Spring MVC auto-configuration 查看官方文档: Spring Boot为Spring MVC提供了自动配置,适用于大多数应用程序. 自动配置在Spring的默认值之上添 ...

  9. springboot08(springmvc自动配置原理)

    MVC WebMvcAutoConfiguration.java @ConditionalOnMissingBean(name = "viewResolver", value = ...

  10. Springboot MVC 自动配置

    Springboot MVC 自动配置 官方文档阅读 https://docs.spring.io/spring-boot/docs/current/reference/html/web.html#w ...

随机推荐

  1. Fiddler手机端抓包环境设置与过滤(二)

    经过了上一篇,我们已经配好了PC与手机端的抓包环境可以实现抓包.传送机:https://www.cnblogs.com/jc-home/p/11668712.html 但是如果不经过筛选的话抓到的内容 ...

  2. U盘安装Proxmox VE(二)

    转自我的个人博客<U盘安装Proxmox VE(二)> 上一篇<U盘安装Proxmox VE(一)>制作好启动盘后,插入U盘,设置bios从U盘启动,开始安装pve. 一.安装 ...

  3. 也谈解决Combobox绑定数据后取值出现System.Data.DataRowView的问题

    刚才遇到一个怪现象:同一个窗口,同一张表,通过第一个Combobox值的改变,动态绑定第二个Combobox,结果出现一个怪现象,第一个Combobox有的值改变第二个Combobox一切正常,有几个 ...

  4. 威联通(NAS)应用篇:自建OwnCloud网盘(百度网盘,拜拜~~~)

    基础环境: 威联通一台 已安装好 ContainerStation 公网 IP(非必须) 自有公网域名 下载镜像文件 提醒:建议先把威联通的自带镜像源改为国内的阿里云镜像源,教程:https://ww ...

  5. 支付宝小程序云开发(Serverless)

    支付宝小程序云开发(Serverless) 博客说明 文章所涉及的资料来自互联网整理和个人总结,意在于个人学习和经验汇总,如有什么地方侵权,请联系本人删除,谢谢! 一.在支付宝账号里面开通小程序云服务 ...

  6. 现代企业要求上什么样的MES(四)

    一个制造企业要想盈利,在生产方面要做的无非是提高资源利用效率和缩短生产通过时间(生产周期),而实现这俩步骤需要生产状况的在线透明及避免薄弱环节的分析数据,由此达到改善生产状态的目的.在erp系统中,通 ...

  7. spring源码解析--上

    本文是作者原创,版权归作者所有.若要转载,请注明出处. 首先是配置类 package com.lusai.config; import org.springframework.context.anno ...

  8. css布局的漂浮、position定位

    float: left:文本流向对象的右边 right:文本流向对象的左边 div具有自动换行效果 position: absolute:将对象从文档流中拖出,即飘到最上面一层,形成层叠,不占位置:可 ...

  9. spring boot构建restful服务

    使用spring boot快速构建出restful服务 JPA实现REST 创建spring boot项目,在项目文件pom.xml中添加以下依赖: <dependency> <gr ...

  10. 「雕爷学编程」Arduino动手做(21)——激光开关模块

    37款传感器与模块的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是不止37种的.鉴于本人手头积累了一些传感器和模块,依照实践出真知(一定要动手做)的理念,以学习和交流为目的,这里 ...