SpringBoot2.0 以上 WebMvcConfigurerAdapter 方法过时 解决办法

176 篇文章 6 订阅

SpringBoot2.0 是基于 spring 5.0 实现的。

在Spring 5.0 中,已经将 WebMvcConfigurerAdapter 抽象类加上 @Deprecated 注解 记为过时。

下面的代码就是过时方法:

package com.handlerinterceptor_demo.handlerinterceptor_demo.config;
 
import com.handlerinterceptor_demo.handlerinterceptor_demo.interceptors.UserIDHandlerInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
 
/**
 * @author 
 */
 
@Configuration
public class WebAppConfig extends WebMvcConfigurerAdapter{
 
    @Autowired
    private UserIDHandlerInterceptor userIDHandlerInterceptor;
 
    /**
     * addPathPatterns 添加拦截规则
     * excludePathPatterns 排除拦截规则
     *
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(userIDHandlerInterceptor).addPathPatterns("/**/*");
    }
}
 

我们来看看 WebMvcConfigurerAdapter  方法,上面已经加了@Deprecated 注解

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
 
package org.springframework.web.servlet.config.annotation;
 
import java.util.List;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.lang.Nullable;
import org.springframework.validation.MessageCodesResolver;
import org.springframework.validation.Validator;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.HandlerExceptionResolver;
 
/** @deprecated */
@Deprecated
public abstract class WebMvcConfigurerAdapter implements WebMvcConfigurer {
    public WebMvcConfigurerAdapter() {
    }
 
    public void configurePathMatch(PathMatchConfigurer configurer) {
    }
 
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
    }
 
    public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
    }
 
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
    }
 
    public void addFormatters(FormatterRegistry registry) {
    }
 
    public void addInterceptors(InterceptorRegistry registry) {
    }
 
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
    }
 
    public void addCorsMappings(CorsRegistry registry) {
    }
 
    public void addViewControllers(ViewControllerRegistry registry) {
    }
 
    public void configureViewResolvers(ViewResolverRegistry registry) {
    }
 
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
    }
 
    public void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnValueHandlers) {
    }
 
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    }
 
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
    }
 
    public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
    }
 
    public void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
    }
 
    @Nullable
    public Validator getValidator() {
        return null;
    }
 
    @Nullable
    public MessageCodesResolver getMessageCodesResolver() {
        return null;
    }
}
 

现在我们采用新的方法来处理:

 

 

方法一(官方推介):

我们看到,WebMvcConfigurerAdapter  抽象类实现了 WebMvcConfigurer 接口,这里我们只需要将 extends WebMvcConfigurerAdapter  替换为 implements WebMvcConfigurer 即可。代码如下:

package com.handlerinterceptor_demo.handlerinterceptor_demo.config;
 
import com.handlerinterceptor_demo.handlerinterceptor_demo.interceptors.UserIDHandlerInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
 
/**
 * @author 
 */
 
@Configuration
public class WebAppConfig implements WebMvcConfigurer{
 
    @Autowired
    private UserIDHandlerInterceptor userIDHandlerInterceptor;
 
    /**
     * addPathPatterns 添加拦截规则
     * excludePathPatterns 排除拦截规则
     *
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(userIDHandlerInterceptor).addPathPatterns("/**/*");
    }

 

方法二(不推介,也过期了):

继承 WebMvcConfigurationSupport,代码如下:

package com.handlerinterceptor_demo.handlerinterceptor_demo.config;
 
import com.handlerinterceptor_demo.handlerinterceptor_demo.interceptors.UserIDHandlerInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
 
/**
 * @author 
 */
 
@Configuration
public class WebAppConfig extends WebMvcConfigurationSupport{
 
    @Autowired
    private UserIDHandlerInterceptor userIDHandlerInterceptor;
 
    /**
     * addPathPatterns 添加拦截规则
     * excludePathPatterns 排除拦截规则
     *
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(userIDHandlerInterceptor).addPathPatterns("/**/*");
        super.addInterceptors(registry);
    }
}
PS:Java 毕竟单继承多实现,而且实现接口也是官方推介的做法。所以推介方法一。
————————————————
版权声明:本文为CSDN博主「java_代码搬运工」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_38164123/article/details/80392904

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值