WebMvcConfigurer配置類其實(shí)是Spring內(nèi)部的一種配置方式,采用JavaBean的形式來代替?zhèn)鹘y(tǒng)的xml配置文件形式進(jìn)行針對(duì)框架個(gè)性化定制。基于java-based方式的spring mvc配置,需要?jiǎng)?chuàng)建一個(gè)配置類并實(shí)現(xiàn)WebMvcConfigurer 接口,WebMvcConfigurerAdapter 抽象類是對(duì)WebMvcConfigurer接口的簡(jiǎn)單抽象(增加了一些默認(rèn)實(shí)現(xiàn)),但在在SpringBoot2.0及Spring5.0中WebMvcConfigurerAdapter已被廢棄 。官方推薦直接實(shí)現(xiàn)WebMvcConfigurer或者直接繼承WebMvcConfigurationSupport,方式一實(shí)現(xiàn)WebMvcConfigurer接口(推薦),方式二繼承WebMvcConfigurationSupport類,具體實(shí)現(xiàn)可看這篇文章。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();}
接下來我們著重找?guī)讉€(gè)方法講解一下:
/* 攔截器配置 */void addInterceptors(InterceptorRegistry var1);/* 視圖跳轉(zhuǎn)控制器 */void addViewControllers(ViewControllerRegistry registry);/** *靜態(tài)資源處理**/void addResourceHandlers(ResourceHandlerRegistry registry);/* 默認(rèn)靜態(tài)資源處理器 */void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer);/** * 這里配置視圖解析器 **/void configureViewResolvers(ViewResolverRegistry registry);/* 配置內(nèi)容裁決的一些選項(xiàng)*/void configureContentNegotiation(ContentNegotiationConfigurer configurer);
|
新聞熱點(diǎn)
疑難解答
圖片精選