当前位置 主页 > 服务器问题 > Linux/apache问题 >

    Spring Boot配置接口WebMvcConfigurer的实现

    栏目:Linux/apache问题 时间:2019-11-22 10:33

    WebMvcConfigurer配置类其实是Spring内部的一种配置方式,采用JavaBean的形式来代替传统的xml配置文件形式进行针对框架个性化定制。基于java-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);