springboot攔截器@Autowired爲null解決

問題緣由

攔截器加載的時間點在springcontext以前,因此在攔截器中注入天然爲nulljava

文件解決

在spring配置文件中這樣寫spring

 @Bean
    public HandlerInterceptor getMyInterceptor(){
        return new MyInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(getMyInterceptor());
        super.addInterceptors(registry);
    }

  

 

使用bean註解提早加載,便可成功。ide

相關文章
相關標籤/搜索