问题
在Interceptor中通过@Autowired注入service报空指针错误。
解决方案
@Configuration
public class MyWebAppConfigurer implements WebMvcConfigurer {
/**
* 将拦截器作为bean写入配置中
* @return
*/
@Bean
public MyInterceptor myInterceptor() {
return new MyInterceptor();
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
/*
* 多个拦截器组成一个拦截器链;
* addPathPatterns用于添加拦截规则;
* excludePathPatterns用户排除拦截;
* 对来自/** 全路径请求进行拦截
*/
registry.addInterceptor(myInterceptor()).addPathPatterns("/**");
}
}