SpringBoot---关于 WebMvcConfigurerAdapter 过时问题及解决方法
SpringBoot---关于 WebMvcConfigurerAdapter 过时问题及解决方法
环境:
IDEA :2020.1
Maven:3.5.6
SpringBoot: 2.3.2
在SpringBoot 1.0 + 中,WebMvcConfigurerAdapter是Spring内部的一种配置方式,采用JavaBean的形式来代替传统的xml配置文件形式进行针对框架个性化定制。实现视图控制,静态资源处理,添加拦截器等一系列操作。
常用方法有:
//添加拦截器
public void addInterceptors(InterceptorRegistry registry) {
}
//添加静态资源处理
public void addResourceHandlers(ResourceHandlerRegistry registry) {
}
//添加视图控制
public void addViewControllers(ViewControllerRegistry registry) {
}
但是, WebMvcConfigurerAdapter 在 SpringBoot 2.0 + (Spring 5.0 +)中显示已经过时,虽然还可以使用,官方不推荐使用。

它的替代方案有两种:
- 继承WebMvcConfigurationSupport 类(继承这个类后,SpringMVC自动配置失效,相当于全面接管SpringMVC )
- 实现 WebMvcConfigurer 接口
由于使用第一种会违背我们使用 SpringBoot 的初衷,增大工作量。
第二种方法其实和原来 WebMvcConfigurerAdapter 使用方法类似 ,只是改了接口名,下面介绍第二种方法:
@Configuration
public class mvcConfig /*extends WebMvcConfigurerAdapter*/ implements WebMvcConfigurer { //WebMvcConfigurerAdapter已过时
//已过时
/*//拓展SpringMVC
@Override
public void addViewControllers(ViewControllerRegistry registry) {
//super.addViewControllers(registry);
registry.addViewController("/newhello").setViewName("hello");
}
//所有的WebMvcConfigurerAdapter组件都会一起起作用
@Bean //将组件注册在容器
public WebMvcConfigurerAdapter webMvcConfigurerAdapter(){
WebMvcConfigurerAdapter adapter = new WebMvcConfigurerAdapter() {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("login");
registry.addViewController("/login.html").setViewName("login");
}
};
return adapter;
}*/
@Bean //将组件注册在容器
public WebMvcConfigurer webMvcConfigurer(){
WebMvcConfigurer webMvcConfigurer = new WebMvcConfigurer() {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("login");
registry.addViewController("/index.html").setViewName("login");
registry.addViewController("/main.html").setViewName("dashboard");
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
InterceptorRegistration interceptor = registry.addInterceptor(new LoginHandlerinterceptor());
interceptor.addPathPatterns("/**");
interceptor.excludePathPatterns("/","/index.html","/user/login");
}
// @Override 并未拦截默认文件的静态资源,不需要进行处理
// public void addResourceHandlers(ResourceHandlerRegistry registry) {
// registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
// }
};
return webMvcConfigurer;
}
}
SpringBoot---关于 WebMvcConfigurerAdapter 过时问题及解决方法的更多相关文章
- springboot springmvc 抛出全局异常解决方法
springboot中抛出异常,springboot自带的是springmvc框架,这个就不多说了. springmvc统一异常解决方法这里要说明的是.只是结合了springboot的使用而已.直接上 ...
- SpringBoot自定义servlet、注册自定义的servlet、过滤器、监听器、拦截器、切面、webmvcconfigureradapter过时问题
[转]https://www.cnblogs.com/NeverCtrl-C/p/8191920.html 1 servlet简介 servlet是一种用于开发动态web资源的技术 参考博客:serv ...
- 【SpringBoot】拦截器使用@Autowired注入接口为null解决方法
最近使用SpringBoot的自定义拦截器,在拦截器中注入了一个DAO,准备下面作相应操作,拦截器代码: public class TokenInterceptor implements Handle ...
- spring boot 添加拦截器的简单实例(springBoot 2.x版本,添加拦截器,静态资源不可访问解决方法)
spring中拦截器主要分两种,一个是HandlerInterceptor,一个是MethodInterceptor 一.HandlerInterceptor HandlerInterceptor是s ...
- springboot使用fastjson中文乱码解决方法 【转载】
以前使用fastjson替换jackson时,没有直接在页面打印过json,都是js使用没有出现乱码,偶然 打印出来出现了中文乱码 之前使用的配置方式 @Configuration public cl ...
- SpringBoot拦截器中无法注入bean的解决方法
SpringBoot拦截器中无法注入bean的解决方法 在使用springboot的拦截器时,有时候希望在拦截器中注入bean方便使用 但是如果直接注入会发现无法注入而报空指针异常 解决方法: 在注册 ...
- Springboot中关于跨域问题的一种解决方法
前后端分离开发中,跨域问题是很常见的一种问题.本文主要是解决 springboot 项目跨域访问的一种方法,其他 javaweb 项目也可参考. 1.首先要了解什么是跨域 由于前后端分离开发中前端页面 ...
- 记一次SpringBoot 开发中所遇到的坑和解决方法
记一次SpringBoot 开发中所遇到的坑和解决方法 mybatis返回Integer为0,自动转型出现空指针异常 当我们使用Integer去接受数据库中表的数据,如果返回的数据中为0,那么Inte ...
- springboot打包成jar包后找不到xml,找不到主类的解决方法
springboot打包成jar包后找不到xml,找不到主类的解决方法 请首先保证你的项目能正常运行(即不打包的时候运行无误),我们在打包时经常遇到如下问题: springboot打包成jar包后找不 ...
随机推荐
- Nginx使用SSL模块配置https
背景 开发微信小程序,需要https域名,因此使用Nginx的SSL模块配置https 步骤 一.去域名管理商(如腾讯云.阿里云等)申请CA证书 二.在Nginx中配置,一般情况下域名管理商会提供配置 ...
- django-模板之标签
目录 模板 模版是纯文本文件,可以生成任何基于文本的文件格式,比如HTML,XML,CSV等.Django模版语言致力于在性能和简单性上取得平衡.Django的模版系统并不是简单的将Python嵌入到 ...
- 遇到的spring问题,或许需要下载新的java jdk
org.springframework.context.support.AbstractApplicationContext
- LQB2013A04倒置的标签
这个题,一开始犯了一个很幼稚的错误 贴贴代码 #include<iostream> #include<stdio.h> #include<stdlib.h> #in ...
- 社交网站的数据挖掘与分析pdf版本|网盘下载地址附提取码|
点击此处进入网盘下载地址 提取码:btqx 作者介绍: 马修·罗塞尔(MatthewA.Russell),DigitalReasoningSystems公司的技术副总裁和Zaffra公司的负责人,是热 ...
- 美团Leaf——全局序列生成器
Leaf的Github地址: https://github.com/Meituan-Dianping/Leaf Leaf美团技术团队博客地址: https://tech.meituan.com/201 ...
- 使用Esxi虚拟化部署OpenWrt/HomeLede+扩容硬盘 保姆级教程
本文介绍使用VMware虚拟化平台部署OpenWrt/HomeLede,并扩容固件硬盘的方法. 推荐使用虚拟化方式部署软路由,理由如下: 部署.升级.回退.扩容等操作非常方便,特别适合折腾 可以方便的 ...
- PHP array_flip() 函数
------------恢复内容开始------------ 实例 反转数组中的键名和对应关联的键值: <?php$a1=array("a"=>"red&qu ...
- Python 字典(Dictionary) clear()方法
Python 字典(Dictionary) clear()方法 描述 Python 字典(Dictionary) clear() 函数用于删除字典内所有元素.高佣联盟 www.cgewang.com ...
- PHP zip_entry_read() 函数
定义和用法 zip_entry_read() 函数从打开的 zip 档案中获取内容.高佣联盟 www.cgewang.com 如果成功,该函数则返回项目的内容.如果失败,则返回 FALSE. 语法 z ...