SpringBoot Web开发(3) WebMvcConfigurerAdapter过期替代方案
springboot2.0中 WebMvcConfigurerAdapter过期替代方案
最近在学习尚硅谷的《springboot核心技术篇》,项目中用到SpringMVC的自动配置和扩展配置。
老师用的是SpringBoot 1.5.9.RELEASE ,他这里扩展SpringMVC用的是WebMvcConfigurerAdapter,
我用的是SpringBoot 2.1.2.RELEASE,在使用WebMvcConfigurerAdapter时,idea提示这个类已经过时了:

网上查资料了解到在springboot 2.0以后 WebMvcConfigurerAdapter 这个方法就已经过时。
上springboot官网查了一下相关资料,下图是springboot1.5.19版本的参考文档,他建议使用WebMvcConfigurerAdapter进行springmvc的自主配置。

然后再看一下最新版本springboot2.1.2版本的参考文档,这里的WebMvcConfigurerAdapter就被WebMvcConfigurer替代了。

下面来说一下解决方案:
springboot1.0中的方法已过时:
//已过时方法:
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("success");
}
}
springboot2.0中的解决方案:
//方法一: 实现WebMvcConfigurer接口
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("success");
}
}
// 方法二: 继承WebMvcConfigurationSupport类
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport{
}
注意:
上面两种方法的不同点在于: 方法一中我们进行的springmvc的自主配置和springboot默认的配置同时生效,
而方法二相当于springboot1.0版本中添加了@EnableWebMvc 注解,自主全面控制springmvc的配置,springboot 不为我们导入 默认配置。
然后分析分析其中的原理:
方法一解析:
源代码摘要如下:
/*首先之前的WebMvcConfigurerAdapter实现的就是WebMvcConfigurer ,它是作为一个适配器存在的。
在springboot1.0的版本是通过继承WebMvcConfigurerAdapter去实现WebMvcConfigurer的相关方法。
而在springboot2.0中我们可以直接去实现WebMvcConfigurer的相关方法。
*/
@Deprecated
public abstract class WebMvcConfigurerAdapter implements WebMvcConfigurer {
public WebMvcConfigurerAdapter() {
}
public void configurePathMatch(PathMatchConfigurer configurer) {
}
.....
public void addResourceHandlers(ResourceHandlerRegistry registry) {
}
public void addCorsMappings(CorsRegistry registry) {
}
.....
}
public interface WebMvcConfigurer {
default void configurePathMatch(PathMatchConfigurer configurer) {
}
......
default void addResourceHandlers(ResourceHandlerRegistry registry) {
}
default void addCorsMappings(CorsRegistry registry) {
}
.....
}
方法二解析:
WebMvc的自动配置都在WebMvcAutoConfiguration类中。
源代码摘要如下:
@ConditionalOnClass({Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class})
//容器中没有检测到WebMvcConfigurationSupport类时,自动配置生效
//所以使用继承WebMvcConfigurationSupport类的方法时,springboot默认的自动配置全部失效。
@ConditionalOnMissingBean({WebMvcConfigurationSupport.class})
@AutoConfigureOrder(-2147483638)
@AutoConfigureAfter({DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class, ValidationAutoConfiguration.class})
public class WebMvcAutoConfiguration {
public static final String DEFAULT_PREFIX = "";
public static final String DEFAULT_SUFFIX = "";
private static final String[] SERVLET_LOCATIONS = new String[]{"/"};
public WebMvcAutoConfiguration() {
}
....
}
总结
WebMvcConfigurerAdapter过期替代方案有两种:
A.实现WebMvcConfigurer接口
这种方法springboot对springmvc的自动配置和我们自己对springmvc的扩展配置都会生效。
B. 继承WebMvcConfigurationSupport类
这种方法springboot对springmvc的自动配置会失效,需要我们自己全面接管springmvc的配置。
SpringBoot Web开发(3) WebMvcConfigurerAdapter过期替代方案的更多相关文章
- SpringBoot Web开发(5) 开发页面国际化+登录拦截
SpringBoot Web开发(5) 开发页面国际化+登录拦截 一.页面国际化 页面国际化目的:根据浏览器语言设置的信息对页面信息进行切换,或者用户点击链接自行对页面语言信息进行切换. **效果演示 ...
- SpringBoot Web开发(4) Thymeleaf模板与freemaker
SpringBoot Web开发(4) Thymeleaf模板与freemaker 一.模板引擎 常用得模板引擎有JSP.Velocity.Freemarker.Thymeleaf SpringBoo ...
- 【SpringBoot】SpringBoot Web开发(八)
本周介绍SpringBoot项目Web开发的项目内容,及常用的CRUD操作,阅读本章前请阅读[SpringBoot]SpringBoot与Thymeleaf模版(六)的相关内容 Web开发 项目搭建 ...
- springboot web开发【转】【补】
pom.xml引入webjars的官网 https://www.webjars.org/ https://www.thymeleaf.org/doc/tutorials/3.0/usingthymel ...
- SpringBoot(四): SpringBoot web开发 SpringBoot使用jsp
1.在SpringBoot中使用jsp,需要在pom.xml文件中添加依赖 <!--引入Spring Boot内嵌的Tomcat对JSP的解析包--> <dependency> ...
- Spring-boot -Web开发
1).创建SpringBoot应用,选中我们需要的模块: 2).SpringBoot已经默认将这些场景配置好了,只需要在配置文件中指定少量配置就可以运行起来 3).自己编写业务代码: 文件名的功能 x ...
- SpringBoot——Web开发(静态资源映射)
静态资源映射 SpringBoot对于SpringMVC的自动化配置都在WebMVCAutoConfiguration类中. 其中一个静态内部类WebMvcAutoConfigurationAdapt ...
- [SpringBoot——Web开发(使用Thymeleaf模板引擎)]
[文字只能描述片段信息,具体细节参考代码] https://github.com/HCJ-shadow/SpringBootPlus 引入POM依赖 <properties> <ja ...
- web开发-CORS支持
一.简介 Web 开发经常会遇到跨域问题,解决方案有:jsonp,iframe,CORS 等等 1.1.CORS与JSONP相比 1.JSONP只能实现GET请求,而CORS支持所有类型的HTTP请求 ...
随机推荐
- Hadoop经典案例(排序&Join&topk&小文件合并)
①自定义按某列排序,二次排序 writablecomparable中的compareto方法 ②topk a利用treemap,缺点:map中的key不允许重复:https://blog.csdn.n ...
- Canvas---clearRect()清除圆形区域
function clearArcFun(x,y,r,cxt){ //(x,y)为要清除的圆的圆心,r为半径,cxt为context var stepClear=1;//别忘记这一步 clearArc ...
- Android动画-View动画
View动画 Android动画分为三类:View动画,帧动画,和属性动画.帧动画也是View动画的一种. View动画的作用对象是View,之所以强调这一点是因为其作用对象有别于Android的另一 ...
- faker模块
faker模块中有很多函数,可以直接生成手机号,身份证号,姓名等 1.安装faker pip install faker 2.faker的使用 from faker import Faker f=Fa ...
- 【未解决】对于使用Windows的IDEA进行编译的文件,但无法在Linux系统中统计代码行数的疑问
在我学习使用Windows的IDEA的过程中,将代码文件转移到Linux虚拟机当中,但无法在Linux系统中统计代码行数. 注意:拷贝进虚拟机的文件均能编译运行. 具体过程如下: root@yogil ...
- c++中关于预编译头的设置问题
在运行代码时会遇到缺少预编译pch.c 或者stadfx.h之类的, 这个时候,先查看有没有包含, 然后看一下预编译头设置中, 是否正确设置.
- javascript 统计字符串中每个字符出现的次数
var str = "abdcadfasfdbadfafdasdfasyweroweurowqrewqrwqrebwqrewqrejwq;;"; // console.log(nu ...
- 利用教育邮箱注册JetBrains产品(pycharm、idea等)的方法
转载:http://www.cnblogs.com/wang-meng/p/8887436.html 1,申请邮箱 地址为:http://mdu.edu.rs/ 邮箱的前缀可以改成自己喜欢的字符 ...
- pacbio bax.h5文件处理及ccs计算
1.NCBI文件格式如下: 2.格式转换 (1) bas.h5 -> ccs source /share/nas2/genome/biosoft/smrtanalysis/2.3.0/smrta ...
- 使用pip命令自动生成项目安装依赖清单
Python项目中经常会带requirements.txt文件,里面是项目所依赖的包的列表,也就是依赖关系清单,这个清单也可以使用pip命令自动生成. pip命令: 1 pip freeze > ...