javascript
SpringBoot2.0 以上 WebMvcConfigurerAdapter 方法过时 解决办法
SpringBoot2.0 是基于 spring 5.0 實現(xiàn)的。
在Spring 5.0 中,已經(jīng)將?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 添加攔截規(guī)則
? ? ?* excludePathPatterns 排除攔截規(guī)則
? ? ?*
? ? ?* @param registry
? ? ?*/
? ? @Override
? ? public void addInterceptors(InterceptorRegistry registry) {
? ? ? ? registry.addInterceptor(userIDHandlerInterceptor).addPathPatterns("/**/*");
? ? }
}
?
我們來看看?WebMvcConfigurerAdapter??方法,上面已經(jīng)加了@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;
? ? }
}
?
現(xiàn)在我們采用新的方法來處理:
?
?
方法一(官方推介):
我們看到,WebMvcConfigurerAdapter??抽象類實現(xiàn)了?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 添加攔截規(guī)則
? ? ?* excludePathPatterns 排除攔截規(guī)則
? ? ?*
? ? ?* @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 添加攔截規(guī)則
? ? ?* excludePathPatterns 排除攔截規(guī)則
? ? ?*
? ? ?* @param registry
? ? ?*/
? ? @Override
? ? public void addInterceptors(InterceptorRegistry registry) {
? ? ? ? registry.addInterceptor(userIDHandlerInterceptor).addPathPatterns("/**/*");
? ? ? ? super.addInterceptors(registry);
? ? }
}
PS:Java 畢竟單繼承多實現(xiàn),而且實現(xiàn)接口也是官方推介的做法。所以推介方法一。
————————————————
版權(quán)聲明:本文為CSDN博主「java_代碼搬運工」的原創(chuàng)文章,遵循CC 4.0 BY-SA版權(quán)協(xié)議,轉(zhuǎn)載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/qq_38164123/article/details/80392904
總結(jié)
以上是生活随笔為你收集整理的SpringBoot2.0 以上 WebMvcConfigurerAdapter 方法过时 解决办法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mooe是什么意思中文(mooe是什么意
- 下一篇: 借读生是什么意思呢(借读生是什么意思)