WebMvcConfigurerAdapter类被弃用后的两种选择

# WebMvcConfigurerAdapter类型被弃用后的选择

1. 介绍

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

2. 出现警告的缘由

若是咱们使用Spring 5.xx(或者Spring Boot 2.xx)版原本构建或者升级应用程序,在配置WebMvc时,则会出现此警告,这是由于在早期的Spring版本中,若是要配置Web应用程序,能够经过扩展WebMvcConfigurerAdapter类快熟实现配置,大体代码以下:java

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 中给接口定义一个默认方法的简单实现:web

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

3. 解决方案

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

/**
 * 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()的调用代码清除。mvc

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

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对扩展类进行注释),示例代码以下:async

/**
 * 消除警告的第二种配置选择
 * @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类被弃用的严重警告。本次技术分享到这里就结束了,感谢你耐心的阅读。若是你在遇到一样问题时还有更好的解决方案,能够在下方的评论区给我留言。ide

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

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

相关文章
相关标签/搜索