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