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. ALV基础二:ALV的扩展功能

    https://www.cnblogs.com/sapSB/p/8532072.html 参考这个,做扩展,有些人喜欢用OO ALV有些喜欢用普通的CALL FUNC ALV...我只用LVC... ...

  2. MAC 下 Excel打开UTF-8格式的文件乱码

    为了识别 Unicode 文件,Microsoft 建议所有的 Unicode 文件应该以 ZERO WIDTH NOBREAK SPACE字符开头.这作为一个”特征符”或”字节顺序标记(byte-o ...

  3. Vim 安装、配置及复制粘贴操作

    1.安装:sudo apt-get install vim 2.配置:cd ~ #进入用户主目录 touch .vimrc #.后缀文件不可见 vi .vimrc #打开文件 输入: set cind ...

  4. c函数创建文件和路径

    bool NewFileName(const char* filename) { size_t len; < (len = strlen(filename))) { char* tmpbuf, ...

  5. [HDU6146]Pokémon GO

    Problem 有一个2n的方格矩阵 在一个格子上可以往旁边8个方向走(如果有格子),求有多少方案把2n走完 Solution 我们用Fi表示从一个角出发走遍所有格子回到这一列另外一点的方案数 显然, ...

  6. httpd-2.4安装配置

    接收请求的类型: 并发访问响应模型: 单进程I/O模型:启动一个进程处理用户请求,这意味着一次只能处理一个请求,多个请求被串行响应: 多进程I/O模型:由父进程并行启动多个子进程,每个子进程响应一个请 ...

  7. [Leetcode 134]汽车加油站 Gas Station (环形)

    [题目] There are N gas stations along a circular route, where the amount of gas at station i is gas[i] ...

  8. web roadmap

  9. go example

    一个简单的博客系统 https://github.com/pingguoxueyuan/gostudy/tree/master/blogger

  10. JPA问题汇总

    JPATools生成的实体类对大小写敏感.如果一个数据库中大小写混乱,生成的实体类也会大小写都有.这种情况会导致HibernateSQL语句查询,每次都会查询第一个大小写字段,所以需要加映射关系.@C ...