服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - 详解SpringBoot 解决拦截器注入Service为空问题

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

2021-05-11 15:40DavidHH Java教程

这篇文章主要介绍了详解SpringBoot 解决拦截器注入Service为空问题的解决,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/**
 *
 * 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;
 }
}

二、自定义拦截器配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/**
 *
 * 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);
 }
}

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

原文链接:https://segmentfault.com/a/1190000015413584

延伸 · 阅读

精彩推荐