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接口
@Co

本文介绍了SpringBoot 2.0中WebMvcConfigurerAdapter已过时的情况,提供了两种新的替代方案:实现WebMvcConfigurer接口和继承WebMvcConfigurationSupport类。这两种方法分别有不同的效果,前者允许自动配置和自定义配置并存,后者则需要全面接管SpringMVC配置。文章还包含了源代码解析来帮助理解这两种方法的工作原理。
最低0.47元/天 解锁文章
945

被折叠的 条评论
为什么被折叠?



