springboot拦截器注入service为空

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

@Configuration
public class WebMvcConfigurer extends WebMvcConfigurationSupport {

    @Resource
    private AdminLoginInterceptor adminLoginInterceptor;
    @Resource
    private FrontLoginInterceptor frontLoginInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
//注意这里不要使用 new AdminLoginInterceptor() ,否则就会出现拦截器里无法注入service的问题 registry.addInterceptor(adminLoginInterceptor).addPathPatterns(
"/admin/**"); registry.addInterceptor(frontLoginInterceptor).addPathPatterns("/front/**"); } }
原文地址:https://www.cnblogs.com/suruozhong/p/9621519.html