springboot2.0中 WebMvcConfigurerAdapter过期替代方案

springboot2.0中 WebMvcConfigurerAdapter过期替代方案

最近在学习尚硅谷的《springboot核心技术篇》,项目中用到SpringMVC的自动配置和扩展配置。

老师用的是SpringBoot 1.5.9.RELEASE ,他这里扩展SpringMVC用的是WebMvcConfigurerAdapter

我用的是SpringBoot 2.1.2.RELEASE,在使用WebMvcConfigurerAdapter时,idea提示这个类已经过时了:
kEBSbR.png

网上查资料了解到在springboot 2.0以后 WebMvcConfigurerAdapter 这个方法就已经过时。


上springboot官网查了一下相关资料,下图是springboot1.5.19版本的参考文档,他建议使用WebMvcConfigurerAdapter进行springmvc的自主配置。

kEB9V1.png

然后再看一下最新版本springboot2.1.2版本的参考文档,这里的WebMvcConfigurerAdapter就被WebMvcConfigurer替代了。

kEBCUx.png


下面来说一下解决方案:

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的配置。

(如有错误,请指正。)

猜你喜欢

转载自blog.csdn.net/qq_40393000/article/details/86615719