WebMvcConfigurationSupport、WebMvcConfigurer、@EnableWebMvc
问题:为什么继承了WebMvcConfigurationSupport后有些配置会不生效呢?WebMvcConfigurer又是什么呢?
简介:
SpringBoot帮我们做了很多的事情,但是有的时候会有自定义的Handler,Interceptor,ViewResolver,MessageConverter等,该怎么配置呢?为什么继承了WebMvcConfigurationSupport后有些配置会不生效呢?WebMvcConfigurer又是什么呢?我们继承WebMvcConfigurationSupport可以自定义SpringMvc的配置。
SpringBoot帮我们做了很多的事情,但是有的时候会有自定义的Handler,Interceptor,ViewResolver,MessageConverter等,该怎么配置呢?为什么继承了WebMvcConfigurationSupport后有些配置会不生效呢?WebMvcConfigurer又是什么呢?
1、WebMvcConfigurationSupport
我们继承WebMvcConfigurationSupport可以自定义SpringMvc的配置。
跟踪发现DelegatingWebMvcConfiguration类是WebMvcConfigurationSupport的一个实现类,DelegatingWebMvcConfiguration类的setConfigurers方法可以收集所有的WebMvcConfigurer实现类中的配置组合起来,组成一个超级配置(这些配置会覆盖掉默认的配置)。而@EnableWebMvc又引入了DelegatingWebMvcConfiguration。
什么是DelegatingWebMvcConfiguration?
DelegatingWebMvcConfiguration是对Spring MVC进行配置的一个代理类。它结合缺省配置和用户配置定义Spring MVC运行时最终使用的配置。
private final WebMvcConfigurerComposite configurers = new WebMvcConfigurerComposite();
// 注入一组WebMvcConfigurer,这些WebMvcConfigurer由开发人员提供,或者框架其他部分提供,已经
// 以bean的方式注入到容器中
@Autowired(required = false)
public void setConfigurers(List<WebMvcConfigurer> configurers) {
if (!CollectionUtils.isEmpty(configurers)) {
this.configurers.addWebMvcConfigurers(configurers);
}
}
所以,我们继承了WebMvcConfigurationSupport,而后使用@EnableWebMvc会覆盖掉原来的配置。
2、WebMvcConfigurer
WebMvcConfigurer配置类其实是Spring内部的一种配置方式,采用JavaBean的形式来代替传统的 xml 配置文件形式进行针对框架个性化定制。可以配置多个
WebMvcConfigurer的主要方法有:
- configurePathMatch:配置路由请求规则
- configureContentNegotiation:内容协商配置
- configureAsyncSupport configureDefaultServletHandling:默认静态资源处理器
- addFormatters:注册自定义转化器
- addInterceptors:拦截器配置
- addResourceHandlers:资源处理
- addCorsMappings:CORS配置
- addViewControllers:视图跳转控制器
- configureViewResolvers:配置视图解析
- addArgumentResolvers:添加自定义方法参数处理器
- addReturnValueHandlers:添加自定义返回结果处理器
- configureMessageConverters:配置消息转换器。重载会覆盖默认注册的HttpMessageConverter
- extendMessageConverters:配置消息转换器。仅添加一个自定义的HttpMessageConverter.
- configureHandlerExceptionResolvers:配置异常转换器
- extendHandlerExceptionResolvers:添加异常转化器 getValidator
- getMessageCodesResolver
addInterceptors:拦截器
addInterceptor:需要一个实现HandlerInterceptor接口的拦截器实例
addPathPatterns:用于设置拦截器的过滤路径规则;addPathPatterns("/**")对所有请求都拦截
excludePathPatterns:用于设置不需要拦截的过滤规则
拦截器主要用途:进行用户登录状态的拦截,日志的拦截等。
@Override
public void addInterceptors(InterceptorRegistry registry) {
super.addInterceptors(registry);
registry.addInterceptor(new TestInterceptor()).addPathPatterns("/**").excludePathPatterns("/emp/toLogin","/emp/login","/js/**","/css/**","/images/**");
}
@EnableWebMvc使用方式
配合上MVC 配置类 再加上@EnableWebMvc 就是相当于 屏蔽掉 @EnableAutoConfiguration中的设置
-
使用@EnableWebMvc注解 等于 扩展了WebMvcConfigurationSupport,但是没有重写任何方法
-
使用“extends WebMvcConfigurationSupport”方式(需要添加@EnableWebMvc),会屏蔽掉springBoot的@EnableAutoConfiguration中的设置
-
使用“implement WebMvcConfigurer”可以配置自定义的配置,同时也使用了@EnableAutoConfiguration中的设置
-
使用“implement WebMvcConfigurer + @EnableWebMvc”,会屏蔽掉springBoot的@EnableAutoConfiguration中的设置
@EnableAutoConfiguratio 是什么?
这里的“@EnableAutoConfiguration中的设置”是指,读取 application.properties 或 application.yml 文件中的配置。
所以:
如果需要使用springBoot的@EnableAutoConfiguration中的设置,那么就只需要“implement WebMvcConfigurer”即可。
如果,需要自己扩展同时不使用@EnableAutoConfiguration中的设置,可以选择另外的方式。