springboot拦截器注入service为空

2023-05-03,,

一般都是因为除了在拦截器之外,还需要在拦截器的配置类中,注册拦截器时没有使用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/**");
} }

springboot拦截器注入service为空的相关教程结束。

《springboot拦截器注入service为空.doc》

下载本文的Word格式文档,以方便收藏与打印。