zoukankan      html  css  js  c++  java
  • Interceptor无法用Autowired自动注入Bean

    在上一篇文章里说到指针为空,本质问题是无法注入的问题。

    这里使用的是springboot2,所以用 WebMvcConfigurationSupport 取代了以前的 WebMvcConfigurerAdapter

    在不涉及注入的情况下是这么写的:

    public class AuthInterceptorConfig extends WebMvcConfigurationSupport{
    public void addInterceptors(InterceptorRegistry registry){
    registry.addInterceptor(new AuthInterceptor()).addPathPatterns("/**");
    // .excludePathPatterns("...");
    super.addInterceptors(registry);
    }
    }

    改进后的写法是这样的:
    public class AuthInterceptorConfig extends WebMvcConfigurationSupport{
    @Bean
    AuthInterceptor authInterceptor(){
    return new AuthInterceptor();
    }
    @Override
    public void addInterceptors(InterceptorRegistry registry){
    registry.addInterceptor(authInterceptor()).addPathPatterns("/**");
    super.addInterceptors(registry);
    }
    }
    当然了在拦截器中,接口也是可以愉快的使用的。
  • 相关阅读:
    1.惨不忍睹凌乱的定时任务
    二维码名片
    给定的逗号分隔的数字字符串转换为Table
    sql 列集合转换成逗号分隔的字符类型
    linq 分组
    触发器
    整合思路、步骤
    整合注意事项
    配置文件
    Struts2的线程安全性
  • 原文地址:https://www.cnblogs.com/htsky/p/10593006.html
Copyright © 2011-2022 走看看