WebMvcConfigurationSupport与WebMvcConfigurer的关系
大家从网上及源码注释上查到的解释是,在spring中配置WebMvc时有两种方法,一种是继承WebMvcConfigurationSupport,重写里面相应的方法,还有一种是继承WebMvcConfigurer的子抽象类WebMvcConfigurerAdapter,也是重写里面相应的方法,但是需要在配置类上添加@EnableWebMvc注解。那这两个类直接是什么关系呢?
细心的开发者会发现,WebMvcConfigurationSupport中那些子类可以重写的空方法在WebMvcConfigurer都有,这说明WebMvcConfigurer只是WebMvcConfigurationSupport的一个扩展类,它并没有扩展新功能,只是为让用户更方便安全的添加自定义配置,为什么说是安全呢?因为如果直接继承WebMvcConfigurationSupport,那么用户可以重写默认的配置,如果对原理不是很清楚地开发者不小心重写了默认的配置,springmvc可能相关功能就无法生效,是一种不安全的行为。但如果是继承WebMvcConfigurerAdapter,那么开发者是在默认配置的基础上添加自定义配置,相对来说更安全一些,只不过要多加一个@EnableWebMvc注解。从这个角度来说,最佳实践还是继承WebMvcConfigurerAdapter,如下
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void addInterceptors(InterceptorRegistry registry) {
//添加自定义拦截器
registry.addInterceptor();
}
}
网上说WebMvcConfigurerAdapter已经标记为过时,我目前用的是4.3.10版本还没有这个标识
为什么WebMvcConfigurer实现要加@EnableWebMvc
@EnableWebMvc注解类上导入了DelegatingWebMvcConfiguration类,该类是WebMvcConfigurationSupport的子类,该类除了实例化WebMvcConfigurationSupport实例以外,另一个作用就是收集BeanFactory中所有WebMvcConfigurer的实现,汇集到WebMvcConfigurerComposite中,在WebMvcConfigurationSupport实例化过程中会分别调用这些实现,将相应的实例传入这些实现中,供开发者在此基础上添加自定义的配置。这也就是在WebMvcConfigurerAdapter子类上要加@EnableWebMvc的原因,因为要先实例化WebMvcConfigurationSupport。为什么可以存在多个WebMvcConfigurer的实现?
一般来讲一个应用中一个WebMvcConfigurer的已经足够,设计成收集多个是不是有些多余?从springboot的autoconfigure机制来看并不多余,反而更灵活,比如我要写一个mybatis的AutoConfiguration和JPA的AutoConfiguration,我就可以在不同的AutoConfiguration里面定义一个WebMvcConfigurer的实现,里面只配置与mybatis或JPA相关的配置,这样需要那个启用那个,不需要人工通过注释代码来转换mybatis和JPA,注意:在springboot下自定义的WebMvcConfigurer实现配置类上是不需要添加@EnableWebMvc的,因为springboot已经实例化了WebMvcConfigurationSupport,如果添加了该注解,默认的WebMvcConfigurationSupport配置类是不会生效的,也就是以用户定义的为主,一般建议还是不覆盖默认的好。
WebMvcConfigurationSupport与WebMvcConfigurer的关系的更多相关文章
- spring boot拦截器WebMvcConfigurerAdapter,以及高版本的替换方案(转)
文章转自 http://blog.51cto.com/12066352/2093750 最近项目采用spring icloud,用的spring boot版本是1.5.x的,spring boot 2 ...
- WebMvcConfigurerAdapter详解和过时后的替代方案
一.什么是WebMvcConfigurerAdapter Spring内部的一种配置方式采用JavaBean的形式来代替传统的xml配置文件形式进行针对框架个性化定制 二.WebMvcConfigur ...
- Spring Boot 2.x引入JS,CSS 失效问题
我的SpringBoot版本是2.0,启动后发现页面奇丑无比: 看下目录结构: SpringBoot默认扫描Static文件夹下的文件,这里把CSS,JS以及图片文件都放在了asserts文件夹下. ...
- SpringBoot2.0拦截器 与 1.X版本拦截器 的实现
1.5 版本 先写个拦截器,跟xml配置方式一样,然后将拦截器加入spring容器管理 .接着创建 配置文件类 继承 WebMvcConfigurerAdapter 类,重写父类方法addInter ...
- SpringBoot之HandlerInterceptorAdapter
SpringBoot之HandlerInterceptorAdapter 在SpringBoot中我们可以使用HandlerInterceptorAdapter这个适配器来实现自己的拦截器.这样就 ...
- @EnableWebMvc,WebMvcConfigurationSupport,WebMvcConfigurer和WebMvcConfigurationAdapter区别
@EnableWebMvc是什么 直接看源码,@EnableWebMvc实际上引入一个DelegatingWebMvcConfiguration. @Retention(RetentionPolicy ...
- 8、SpringBoot-CRUD默认访问的首页以及thyleaf的静态文件引入/WebMvcConfigurer / WebMvcConfigurationSupport
1.导入资源 2.默认的访问首页 (1).将代码写在controller中 @RequestMapping({"/","index.html"}) public ...
- Spring boot 梳理 -@SpringBootApplication、@EnableAutoConfiguration与(@EnableWebMVC、WebMvcConfigurationSupport,WebMvcConfigurer和WebMvcConfigurationAdapter)
@EnableWebMvc=继承DelegatingWebMvcConfiguration=继承WebMvcConfigurationSupport 直接看源码,@EnableWebMvc实际上引入一 ...
- WebMvcConfigurer 与 WebMvcConfigurationSupport避坑指南
我们知道,在Spring Boot 2.0后用自己的的配置类继承WebMvcConfigurerAdapter时,idea会提示这个类已经过时了. 通常情况下我们会采用下面两种代替方案: 实现WebM ...
随机推荐
- kubernetes的搭建以及dashboard页面的启动
###查看kubernetes状态 ``` kubectl get pods -A #查看相关状态 kubectl get cs #查看k8s的ready状态 kubectl get node #查看 ...
- 51 Nod 1092 回文字符串
1092 回文字符串 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 收藏 关注 回文串是指aba.abba.cccbccc.aaaa这种左右对称的字符串.每 ...
- [SCOI2009]windy数 代码 (对应数位dp入门)
Code1 (DP版) #include<bits/stdc++.h> #define in(i) (i=read()) using namespace std; int read() { ...
- 关于我&留言板
在下高一OIer一枚,就读于SC的一所发展中学(ruo)校 对二次元什么的,有着淡淡的喜爱 初三的时候入了古风的坑,想变得文艺一点,可爱一点 也会听歌,但听得不多(主要是听新歌比较随缘),范围窄(古风 ...
- c++ gdb调试的基本使用
https://blog.csdn.net/zdy0_2004/article/details/80102076
- 使用Telnet访问端口发送数据
什么是Telnet? 对于Telnet的认识,不同的人持有不同的观点,可以把Telnet当成一种通信协议,但是对于入侵者而言,Telnet只是一种远程登录的工具.一旦入侵者与远程主机建立了Telnet ...
- navicat for mysql安装
搜索一款navicat for mysql然后进行下载. 步骤阅读 2 当我们下载完成之后首先进行数据包的解压,同时可以运行navicat for mysql程序. 破解工具下载:https://pa ...
- This sample is for changing from “float64” to “int” for values did unmarshal using map[string]interface{}. When it did unmarshal using map[string]interface{}, a number with “int” was changed to “floa
This sample is for changing from “float64” to “int” for values did unmarshal using map[string]interf ...
- leetcode-hard-array-287. Find the Duplicate Number
mycode 77.79% class Solution(object): def findDuplicate(self, nums): """ :type nums ...
- STL -- heap结构及算法
STL -- heap结构及算法 heap(隐式表述,implicit representation) 1. heap概述 : vector + heap算法 heap并不归属于STL容器组件,它是个 ...