WebMvcConfigurerAdapter类被弃用后的两种选择

《WebMvcConfigurerAdapter类被弃用后的两种选择》

1. 介绍

在本文中,将介绍将spring 4.xx(或者更低)版本升级到Spring 5.xx以及将Spring Boot 1.xx版本升级到Spring Boot 2.xx版本后会报的一个严重警告:“Warning:The type WebMvcConfigurerAdapter is deprecated.” ,以及快速的分析产生这个严重警告的原因和处理办法。

2. 出现警告的原因

如果我们使用Spring 5.xx(或者Spring Boot 2.xx)版本来构建或者升级应用程序,在配置WebMvc时,则会出现此警告,这是因为在早期的Spring版本中,如果要配置Web应用程序,可以通过扩展WebMvcConfigurerAdapter类快熟实现配置,大致代码如下:

package com.ramostear.page;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * Spring 4(或者Spring Boot 1.x)版本配置Web应用程序示例
 * @author ramostear
 * @create-time 2019/4/18 0018-1:38
 */
@Configuration
public class OldMvcConfig extends WebMvcConfigurerAdapter{

    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        super.configurePathMatch(configurer);
        configurer.setUseSuffixPatternMatch(false);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/static/")
                .addResourceLocations("classpath:/META-INF/resources/")
                .addResourceLocations("classpath:/public/")
                .addResourceLocations("classpath:/resources/");
        super.addResourceHandlers(registry);
    }
}

WebMvcConfigurerAdapter 是一个实现了WebMvcConfigurer 接口的抽象类,并提供了全部方法的空实现,我们可以在其子类中覆盖这些方法,以实现我们自己的配置,如视图解析器,拦截器和跨域支持等…,由于Java的版本更新,在Java 8中,可以使用default关键词为接口添加默认的方法,Spring在升级的过程中也同步支持了Java 8中这一新特性。下面是在Java 8 中给接口定义一个默认方法的简单实现:

public interface MyInterface{
    
    default void sayHello(){
        //...
    }
    
    void sayName(){}
    
    String writeName(){}
    
    //...
}

3. 解决方案

如前面所述,从Spring 5开始,WebMvcConfigure接口包含了WebMvcConfigurerAdapter类中所有方法的默认实现,因此WebMvcConfigurerAdapter这个适配器就被打入冷宫了,下面是WebMvcConfigurerAdapter类部分源码示例:

/**
 * An implementation of {@link WebMvcConfigurer} with empty methods allowing
 * subclasses to override only the methods they're interested in.
 *
 * @author Rossen Stoyanchev
 * @since 3.1
 * @deprecated as of 5.0 {@link WebMvcConfigurer} has default methods (made
 * possible by a Java 8 baseline) and can be implemented directly without the
 * need for this adapter
 */
@Deprecated
public abstract class WebMvcConfigurerAdapter implements WebMvcConfigurer {

    /**
     * {@inheritDoc}
     * <p>This implementation is empty.
     */
    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
    }

    /**
     * {@inheritDoc}
     * <p>This implementation is empty.
     */
    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
    }
    
    ...
}        

趣味提示:我们可以通过实现WebMvcConfigure接口中的方法来配置Web应用程序,而不需要让
WebMvcConfigurerAdapter这个
中间商 赚差价。

如此这般,我们找到了一个消除警告的方法:直接实现WebMvcConfigurer接口。在我们准备与WebMvcConfigurer打交道之前,先看看此接口的基本情况:

public interface WebMvcConfigurer {

    default void configurePathMatch(PathMatchConfigurer configurer) {
    }

    /**
     * Configure content negotiation options.
     */
    default void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
    }

    /**
     * Configure asynchronous request handling options.
     */
    default void configureAsyncSupport(AsyncSupportConfigurer configurer) {
    }
    
   ...
}       

现在,我们就可以动手配置Web应用程序了,大致的代码如下:

/**
 * Spring 5 (或者Spring Boot 2.x)版本配置Web应用程序示例
 * @author ramostear
 * @create-time 2019/4/18 0018-1:40
 */
@Configuration
public class MvcConfigure implements WebMvcConfigurer{

    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        configurer.setUseSuffixPatternMatch(false);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/static/")
                .addResourceLocations("classpath:/public/")
                .addResourceLocations("classpath:/resources/");
    }
}

就这样简单地将警告消除了,将原来的继承WebMvcConfigurerAdapter类改为实现WebMvcConfigurer接口,其余的地方都没有变化。但有一点需要注意,如果你是升级旧有的应用程序,需要将方法中对super()的调用代码清除。

至此,我们的程序又可以愉快的玩耍了。那么,除了消除中间商 赚差价的方式来规避警告外,还有没有其他的途径呢?答案当然是肯定的。我们除了消除中间商从WebMvcConfigurer中获得配置Web应用程序的途径外,还可以直接从WebMvcConfigurationSupport这个配置“供应商“的手中获取配置途径。WebMvcConfigurationSupport是一个提供了以Java编程方式来配置Web应用程序的配置主类,所以我们可以从这个配置供应商的手中获取Web应用程序的配置方式。方法很简单,只需要扩展此类并重写对应的方法即可。和上面的方式一样,我们先看看此类的内部大致结构:

public class WebMvcConfigurationSupport implements ApplicationContextAware, ServletContextAware {
    ...
    /**
     * Provide access to the shared handler interceptors used to configure
     * {@link HandlerMapping} instances with.
     * <p>This method cannot be overridden; use {@link #addInterceptors} instead.
     */
    protected final Object[] getInterceptors() {
        ...
    }
    
    /**
     * Return a handler mapping ordered at Integer.MAX_VALUE-1 with mapped
     * resource handlers. To configure resource handling, override
     * {@link #addResourceHandlers}.
     */
    @Bean
    @Nullable
    public HandlerMapping resourceHandlerMapping() {
        ...
        handlerMapping.setPathMatcher(mvcPathMatcher());
        handlerMapping.setUrlPathHelper(mvcUrlPathHelper());
        handlerMapping.setInterceptors(getInterceptors());
        handlerMapping.setCorsConfigurations(getCorsConfigurations());
        return handlerMapping;
    }
}    

是不是看到很熟悉的东西,有拦截器,静态资源映射等等…,现在我们只需要扩展此类并重写其中的方法,就可以配置我们的Web应用程序(还需要使用@Configuration对扩展类进行注释),示例代码如下:

/**
 * 消除警告的第二种配置选择
 * @author ramostear
 * @create-time 2019/4/7 0007-4:10
 */
@Configuration
public class MvcConfig extends WebMvcConfigurationSupport {

    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        super.configurePathMatch(configurer);
        configurer.setUseSuffixPatternMatch(false);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/static/")
                .addResourceLocations("classpath:/META-INF/resources/")
                .addResourceLocations("classpath:/public/")
                .addResourceLocations("classpath:/resources/");
        super.addResourceHandlers(registry);
    }
}

4. 结束语

在本文中,通过快速的梳理,给出了两种不同的方案来消除由于升级Spring(或者Spring Boot)版本所带来的WebMvcConfigurerAdapter类被弃用的严重警告。本次技术分享到这里就结束了,感谢你耐心的阅读。如果你在遇到同样问题时还有更好的解决方案,可以在下方的评论区给我留言。

<center>原文作者:谭朝红</center>
<center>原文标题:WEBMVCCONFIGURERADAPTER被弃用后的两个选择</center>

原文链接:https://www.ramostear.com/articles/after_deprecated_web_mvc_configurer_adapter.html

    原文作者:谭朝红
    原文地址: https://segmentfault.com/a/1190000018904390
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞