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);
    }
    }
    当然了在拦截器中,接口也是可以愉快的使用的。
  • 相关阅读:
    深入探讨多态性及其在Java中的好处
    可扩展的Java线程池执行器
    Java并发:线程限制
    CF集萃3
    CF1151F
    [欧拉路]CF1152E Neko and Flashback
    LOJ#3119 随机立方体
    UOJ#449 喂鸽子
    CF1140F
    洛谷P5071 此时此刻的光辉
  • 原文地址:https://www.cnblogs.com/htsky/p/10593006.html
Copyright © 2011-2022 走看看