天天看点

解决方案:SpringBoot拦截器注入service为空的问题。问题解决方案

问题

在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("/**");
    }
}
           

继续阅读