java自定义拦截器使用@Autowired时注入失败

问题背景

打算自己写个自定义拦截器,通过配置文件中的开关switch来判断是否对请求的header进行校验;写完了自定义拦截器后,并且测试通过,此时认为拦截器写的是对的并开始写拦截器里面的具体逻辑。写完拦截方法里面的逻辑后发现一直报空指针异常。通过debug查看发现是配置类自动注入为null,导致一直获取不到。(部分代码如下)

WebMvcConfig
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
   
    public void addInterceptors(InterceptorRegistry registry){
   
        registry.addInterceptor(new HandlerInterceptorAdapter()).addPathPatterns("/hello/**");
    }
}
HandlerInterceptorAdapter
public class HandlerInterceptorAdapter implements HandlerInterceptor {
   
    // 日志配置
    

你可能感兴趣的:(其他,spring,boot,java)