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);
    }
    }
    当然了在拦截器中,接口也是可以愉快的使用的。
  • 相关阅读:
    Linux curl命令添加参数
    postman无限循环执行接口用例
    xshell用root用户登录ubuntu
    centos5 yum源配置
    移动端布局方案
    vue + store2实现未提交信息自动保存
    sublime text里的terminal
    20180204
    2018.1.3 interview
    http协议
  • 原文地址:https://www.cnblogs.com/htsky/p/10593006.html
Copyright © 2011-2022 走看看