WebMvcConfigurer配置類其實是Spring內部的一種配置方式,采用JavaBean的形式來代替傳統的xml配置文件形式進行針對框架個性化定制?;趈ava-based方式的spring mvc配置,需要創建一個配置類并實現WebMvcConfigurer 接口,WebMvcConfigurerAdapter 抽象類是對WebMvcConfigurer接口的簡單抽象(增加了一些默認實現),但在在SpringBoot2.0及Spring5.0中WebMvcConfigurerAdapter已被廢棄 。官方推薦直接實現WebMvcConfigurer或者直接繼承WebMvcConfigurationSupport,方式一實現WebMvcConfigurer接口(推薦),方式二繼承WebMvcConfigurationSupport類,具體實現可看這篇文章。https://www.jb51.net/article/174766.htm
//// 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.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; public interface WebMvcConfigurer { void configurePathMatch(PathMatchConfigurer var1); void configureContentNegotiation(ContentNegotiationConfigurer var1); void configureAsyncSupport(AsyncSupportConfigurer var1); void configureDefaultServletHandling(DefaultServletHandlerConfigurer var1); void addFormatters(FormatterRegistry var1); void addInterceptors(InterceptorRegistry var1); void addResourceHandlers(ResourceHandlerRegistry var1); void addCorsMappings(CorsRegistry var1); void addViewControllers(ViewControllerRegistry var1); void configureViewResolvers(ViewResolverRegistry var1); void addArgumentResolvers(List<HandlerMethodArgumentResolver> var1); void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> var1); void configureMessageConverters(List<HttpMessageConverter<?>> var1); void extendMessageConverters(List<HttpMessageConverter<?>> var1); void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> var1); void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> var1); Validator getValidator(); MessageCodesResolver getMessageCodesResolver();}
接下來我們著重找幾個方法講解一下:
/* 攔截器配置 */void addInterceptors(InterceptorRegistry var1);/* 視圖跳轉控制器 */void addViewControllers(ViewControllerRegistry registry);/** *靜態資源處理**/void addResourceHandlers(ResourceHandlerRegistry registry);/* 默認靜態資源處理器 */void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer);/** * 這里配置視圖解析器 **/void configureViewResolvers(ViewResolverRegistry registry);/* 配置內容裁決的一些選項*/void configureContentNegotiation(ContentNegotiationConfigurer configurer);
新聞熱點
疑難解答
圖片精選