只能注入拦截器,而不是new,不能手动new,否则dubbo服务注入不进去,就变成了null了
拦截器配置类
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
@Autowired
JurisdictionInterceptor jurisdictionInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
//注册TestInterceptor拦截器, 只能注入拦截器,而不是new,不能手动new,否则dubbo服务注入不进去
InterceptorRegistration registration = registry.addInterceptor(jurisdictionInterceptor);
registration.addPathPatterns("/**"); //所有路径都被拦截
registration.excludePathPatterns( //添加不拦截路径
);
}
}
拦截器
@Component
public class JurisdictionInterceptor implements HandlerInterceptor {
@DubboReference
private UserService userService;
/**
* @Description: 在请求处理之前进行调用(Controller方法调用之前)
**/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
return true;
}
}