日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

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

發布時間:2024/9/20 javascript 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringBoot2.0 以上 WebMvcConfigurerAdapter 方法过时 解决办法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

總結

以上是生活随笔為你收集整理的SpringBoot2.0 以上 WebMvcConfigurerAdapter 方法过时 解决办法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。