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过期替代方案的更多相关文章

  1. SpringBoot Web开发(5) 开发页面国际化+登录拦截

    SpringBoot Web开发(5) 开发页面国际化+登录拦截 一.页面国际化 页面国际化目的:根据浏览器语言设置的信息对页面信息进行切换,或者用户点击链接自行对页面语言信息进行切换. **效果演示 ...

  2. SpringBoot Web开发(4) Thymeleaf模板与freemaker

    SpringBoot Web开发(4) Thymeleaf模板与freemaker 一.模板引擎 常用得模板引擎有JSP.Velocity.Freemarker.Thymeleaf SpringBoo ...

  3. 【SpringBoot】SpringBoot Web开发(八)

    本周介绍SpringBoot项目Web开发的项目内容,及常用的CRUD操作,阅读本章前请阅读[SpringBoot]SpringBoot与Thymeleaf模版(六)的相关内容 Web开发 项目搭建 ...

  4. springboot web开发【转】【补】

    pom.xml引入webjars的官网 https://www.webjars.org/ https://www.thymeleaf.org/doc/tutorials/3.0/usingthymel ...

  5. SpringBoot(四): SpringBoot web开发 SpringBoot使用jsp

    1.在SpringBoot中使用jsp,需要在pom.xml文件中添加依赖 <!--引入Spring Boot内嵌的Tomcat对JSP的解析包--> <dependency> ...

  6. Spring-boot -Web开发

    1).创建SpringBoot应用,选中我们需要的模块: 2).SpringBoot已经默认将这些场景配置好了,只需要在配置文件中指定少量配置就可以运行起来 3).自己编写业务代码: 文件名的功能 x ...

  7. SpringBoot——Web开发(静态资源映射)

    静态资源映射 SpringBoot对于SpringMVC的自动化配置都在WebMVCAutoConfiguration类中. 其中一个静态内部类WebMvcAutoConfigurationAdapt ...

  8. [SpringBoot——Web开发(使用Thymeleaf模板引擎)]

    [文字只能描述片段信息,具体细节参考代码] https://github.com/HCJ-shadow/SpringBootPlus 引入POM依赖 <properties> <ja ...

  9. web开发-CORS支持

    一.简介 Web 开发经常会遇到跨域问题,解决方案有:jsonp,iframe,CORS 等等 1.1.CORS与JSONP相比 1.JSONP只能实现GET请求,而CORS支持所有类型的HTTP请求 ...

随机推荐

  1. mysql查看配置生效

    mysql> show status like 'Threads%';+-------------------+-------+| Variable_name     | Value |+--- ...

  2. 『TensorFlow』one_hot化标签

    tf.one_hot(indices, depth):将目标序列转换成one_hot编码 tf.one_hot(indices, depth, on_value=None, off_value=Non ...

  3. PIL合并4张图demo 800px以下的居中显示小例子

    from PIL import Image #新建一个空白文件 大小为1600*1600 颜色为白色 newIm= Image.new('RGB', (1600, 1600), 'white') #打 ...

  4. 正则表达式中pw、IDCard和EM匹配

    1密码强度正则 //密码强度正则,最少6位,包括至少1个大写字母,1个小写字母,1个数字,1个特殊字符 var pPattern = /^.*(?=.{6,})(?=.*\d)(?=.*[A-Z])( ...

  5. 枚举Enum 的常用方法

    一.枚举介绍 通常定义常量方法和枚举定义常量方法区别 public class State { public static final int ON = 1; public static final ...

  6. centos重置密码

    重置root密码:   法一:                1.开机按e                2.将linux 16后的ro改为rw init=/sysroot/bin/          ...

  7. 开始Flask项目

    新建Flask项目. 设置调试模式. 理解Flask项目主程序. 使用装饰器,设置路径与函数之间的关系. 使用Flask中render_template,用不同的路径,返回首页.登录员.注册页. 用视 ...

  8. ceph存储集群测试方案

    --测试目的 测试ceph集群的读写性能,根据测试数据了解整个ceph集群的性能情况. --测试环境 1.8节点ceph集群环境,1台虚拟机(cpu 8核,内存8G),8k的块大小,时长2小时 2.8 ...

  9. 外网ssh内网Linux服务器holer实现

    外网ssh访问内网linux 内网的主机上安装了Linux系统,只能在局域网内访问,怎样从公网也能ssh访问本地Linux服务器? 本文将介绍使用holer实现的具体步骤. 1. 准备工作 1.1 安 ...

  10. keil项目的调试与编译

    编译: Translate===编译单个文件 Build====编译当前项目,如果该项目先前编译过1次,并且文件没有编辑改动,则点击时不会重新编译 Rebuild===重新编译,每点击一次就重新编译. ...