欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

详解SpringBoot 解决拦截器注入Service为空问题

程序员文章站 2024-02-13 23:48:10
一、自定义拦截器实现 handlerinterceptor 接口 /** * * created by zhh on 2018/04/20. *...

一、自定义拦截器实现 handlerinterceptor 接口

/**
 * 
 * created by zhh on 2018/04/20.
 */
public class myinterceptor implements handlerinterceptor {

 @autowired
 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 zhh 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.addinterceptors(registry);
 } 
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。