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);
    }
    }
    当然了在拦截器中,接口也是可以愉快的使用的。
  • 相关阅读:
    Oracle数据库5--数据库对象
    Oracle数据库4--多表关联
    Session
    cookie
    Servlet的部分response响应处理
    Servlet的部分request请求处理
    Linux部分命令
    Linux基础
    弹性布局
    animation 动画
  • 原文地址:https://www.cnblogs.com/htsky/p/10593006.html
Copyright © 2011-2022 走看看