zoukankan      html  css  js  c++  java
  • spring boot拦截器WebMvcConfigurerAdapter,以及高版本的替换方案(转)

    文章转自 http://blog.51cto.com/12066352/2093750

    最近项目采用spring icloud,用的spring boot版本是1.5.x的,spring boot 2.0,Spring 5.0 以后WebMvcConfigurerAdapter会取消掉。以下介绍下大体的内容,希望对大家都有所帮助。

    • 以下WebMvcConfigurerAdapter 比较常用的重写接口

    /** 解决跨域问题 **/
    public void addCorsMappings(CorsRegistry registry) ;
    /** 添加拦截器 **/
    void addInterceptors(InterceptorRegistry registry);
    /** 这里配置视图解析器 **/
    void configureViewResolvers(ViewResolverRegistry registry);
    /** 配置内容裁决的一些选项 **/
    void configureContentNegotiation(ContentNegotiationConfigurer configurer);
    /** 视图跳转控制器 **/
    void addViewControllers(ViewControllerRegistry registry);
    /** 静态资源处理 **/
    void addResourceHandlers(ResourceHandlerRegistry registry);
    /** 默认静态资源处理器 **/
    void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer);
    • 新的版本解决方案目前有两种

      方案1 直接实现WebMvcConfigurer

          @Configuration
      public class WebMvcConfg implements WebMvcConfigurer {
      
              @Override
              public void addViewControllers(ViewControllerRegistry registry) {
                      registry.addViewController("/index").setViewName("index");
              }
      
      }

      方案2 直接继承WebMvcConfigurationSupport

          @Configuration
      public class WebMvcConfg extends WebMvcConfigurationSupport {
      
              @Override
              public void addViewControllers(ViewControllerRegistry registry) {
                      registry.addViewController("/index").setViewName("index");
              }
      
      }

    其实,源码下WebMvcConfigurerAdapter是实现WebMvcConfigurer接口,所以直接实现WebMvcConfigurer接口也可以;WebMvcConfigurationSupport与WebMvcConfigurerAdapter、接口WebMvcConfigurer处于同一个目录下WebMvcConfigurationSupport包含WebMvcConfigurer里面的方法,由此看来版本中应该是推荐使用WebMvcConfigurationSupport类的,WebMvcConfigurationSupport应该是新版本中对WebMvcConfigurerAdapter的替换和扩展【个人见解,如果有误,请帮忙纠正】

  • 相关阅读:
    SourceTree使用教程(六)--回滚版本到某次提交
    SourceTree使用教程(四)---冲突解决
    Git 分支合并后回退的几种情况分析
    HTTP认证之基本认证——Basic(二) _
    C#3.0中自动属性和对象初始化器
    C# 3.0新特征之创建和初始化集合对象
    SQL 用多个条件进行排序;以及根据一个条件的多个值,进行排序
    如何修改 .NET Core Kestrel 下的端口
    存储过程
    mysql临时表用法分析【查询结果可存在临时表中】
  • 原文地址:https://www.cnblogs.com/shuaiandjun/p/10306192.html
Copyright © 2011-2022 走看看