springboot拦截器中注入service失败问题

一般都是因为除了在拦截器之外,还需要在拦截器的配置类中,注册拦截器时没有使用spring的bean,而是使用了new创建bean造成的。

 

可参考:http://blog.csdn.net/wmh13262227870/article/details/77005920

当然,部分版本也支持以下方式:

@Configurationpublic 
class WebInterceptorLoader extends WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter{
//这里可以自动注入下interceptor

     @Autowired

private UserAuthInterceptor userAuthInterceptor;


@Override
public void addInterceptors(InterceptorRegistry registry){

//注意这里不要使用 new UserAuthInterceptor() ,否则就会出现拦截器里无法注入service的问题 
registry.addInterceptor(userAuthInterceptor).addPathPatterns("/**").excludePathPatterns("/components/*/*","/user-auth/quit");
}
}

posted @ 2019-02-21 13:41  雪中刀  阅读(3785)  评论(1)    收藏  举报