WebMvcConfigurationSupport和WebMvcConfigurer

栏目: Java · 发布时间: 7年前

内容简介:SpringBoot帮我们做了很多的事情,但是有的时候会有自定义的Handler,Interceptor,ViewResolver,MessageConverter等,该怎么配置呢?为什么继承了WebMvcConfigurationSupport后有些配置会不生效呢?WebMvcConfigurer又是什么呢?我们继承WebMvcConfigurationSupport可以自定义SpringMvc的配置。跟踪发现DelegatingWebMvcConfiguration类是WebMvcConfigurati

SpringBoot帮我们做了很多的事情,但是有的时候会有自定义的Handler,Interceptor,ViewResolver,MessageConverter等,该怎么配置呢?为什么继承了WebMvcConfigurationSupport后有些配置会不生效呢?WebMvcConfigurer又是什么呢?

WebMvcConfigurationSupport

我们继承WebMvcConfigurationSupport可以自定义SpringMvc的配置。

跟踪发现DelegatingWebMvcConfiguration类是WebMvcConfigurationSupport的一个实现类,DelegatingWebMvcConfiguration类的setConfigurers方法可以收集所有的WebMvcConfigurer实现类中的配置组合起来,组成一个超级配置(这些配置会覆盖掉默认的配置)。而@EnableWebMvc又引入了DelegatingWebMvcConfiguration。

所以,我们继承了WebMvcConfigurationSupport,而后使用@EnableWebMvc会覆盖掉原来的配置。

WebMvcConfigurer

WebMvcConfigurer配置类其实是Spring内部的一种配置方式,采用JavaBean的形式来代替传统的xml配置文件形式进行针对框架个性化定制。

在官方文档中有这么一段话:

>

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.

所以,如果我们想要在Auto-configuration的基础上配置自定义的interceptors, formatters, view controllers等功能话,我们可以实现WebMvcConfigurer,并用@Configuration注释。

WebMvcConfigurer的主要方法有:

  • configurePathMatch:配置路由请求规则
  • configureContentNegotiation:内容协商配置
  • configureAsyncSupport
  • configureDefaultServletHandling:默认静态资源处理器
  • addFormatters:注册自定义转化器
  • addInterceptors:拦截器配置
  • addResourceHandlers:资源处理
  • addCorsMappings:CORS配置
  • addViewControllers:视图跳转控制器
  • configureViewResolvers:配置视图解析
  • addArgumentResolvers:添加自定义方法参数处理器
  • addReturnValueHandlers:添加自定义返回结果处理器
  • configureMessageConverters:配置消息转换器。重载会覆盖默认注册的HttpMessageConverter
  • extendMessageConverters:配置消息转换器。仅添加一个自定义的HttpMessageConverter.
  • configureHandlerExceptionResolvers:配置异常转换器
  • extendHandlerExceptionResolvers:添加异常转化器
  • getValidator
  • getMessageCodesResolver

使用方式

  • 使用@EnableWebMvc注解 等于 扩展了WebMvcConfigurationSupport,但是没有重写任何方法
  • 使用“extends WebMvcConfigurationSupport”方式(需要添加@EnableWebMvc),会屏蔽掉springBoot的@EnableAutoConfiguration中的设置
  • 使用“implement WebMvcConfigurer”可以配置自定义的配置,同时也使用了@EnableAutoConfiguration中的设置
  • 使用“implement WebMvcConfigurer + @EnableWebMvc”,会屏蔽掉springBoot的@EnableAutoConfiguration中的设置

这里的“@EnableAutoConfiguration中的设置”是指,读取 application.properties 或 application.yml 文件中的配置。

所以,如果需要使用springBoot的@EnableAutoConfiguration中的设置,那么就只需要“implement WebMvcConfigurer”即可。如果,需要自己扩展同时不使用@EnableAutoConfiguration中的设置,可以选择另外的方式。


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。

Learn Python the Hard Way

Learn Python the Hard Way

Zed Shaw / Example Product Manufacturer / 2011

This is a very beginner book for people who want to learn to code. If you can already code then the book will probably drive you insane. It's intended for people who have no coding chops to build up t......一起来看看 《Learn Python the Hard Way》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

随机密码生成器
随机密码生成器

多种字符组合密码

html转js在线工具
html转js在线工具

html转js在线工具