java怎么拦截某个对象
236
2023-07-28
详解SpringBoot 解决拦截器注入Service为空问题
一、自定义拦截器实现 HandlerInterceptor 接口
/**
*
* Created by zhh on 2018/04/20.
*/
public class MyInterceptor implements HandlerInterceptor {
@Autowiredhttp://
private NetworkProxyInfoService networkProxyInfoService;
@Override
public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
// TODO Auto-generated method stub
}
@Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
throws Exception {
networkProxyInfoService.getAllNetworkProxyInfoByIsValid(GobalConstant.ProxyValid.VALID);
}
@Override
public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
// TODO Auto-generated method stub
return true;
}
}
二、自定义拦截器配置
/**
*
* Created by QwUVJlnVtzhh on 2018/04/20.
*/
@Configuration
public class MyWebMvcConfig extends WebMvcConfigurerAdapter {
/**
* 将自定义拦截器作为Bean写入配置
* @return
*/
@Bean
public MyInterceptor myInterceptor() {
return new MyInterceptor();
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
/**
* 多个拦截器组成一个拦截器链
* addPathPatterns 用于添加拦截规则
* excludePathPatterns 用户排除拦截
*/
registry.addInterceptor(myInterceptor()).addPathPatterns("/**");
super.addInterceptorQwUVJlnVts(registry);
}
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~