使用拦截器调用服务出现空指针异常解决
在使用拦截器的时候,发现调用Service会出现空指针异常,查看原因,原来是
使用new关键字导致无法注入
@Configuration
public class WebConfiguer implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new UserLoginHanderInterceptors()).addPathPatterns("/**");
}
}
改正:
将拦截器加入@Component
自动注入WebConfig

在配置Spring MVC的拦截器时遇到问题,当尝试通过new关键字实例化Service时,引发了空指针异常。解决方案是将拦截器标记为@Component并利用依赖注入。修正后的代码展示了如何正确地在WebConfig中自动注入拦截器,避免了空指针异常的发生。
557

被折叠的 条评论
为什么被折叠?



