SpringBoot的HandlerInterceptor中依赖注入为null
发布于2018-08-15 14:13:48
原因
拦截器加载是在springcontext创建之前完成
解决
让bean提前加载
实例
注意拦截链里面的是你注入到spring的拦截类而不是你实际用的拦截类。我之前查资料的时候就很多人直接把要用的拦截类插入到拦截链里面,还他娘的发出来给别人参考!
@Configuration
public class interceptorConfig extends WebMvcConfigurerAdapter{
//新建一个拦截类注入到spring容器
@Bean
public HandlerInterceptor getMyInterceptor(){
//返回自定义的拦截类
return new MyInterceptor();
}
//将注入到spring中的拦截类添加到拦截链
public void addInterceptors(InterceptorRegistry registry){
registry.addInterceptor(getMyInterceptor()) //指定拦截器类
.addPathPatterns("/**"); //指定该类拦截的url
super.addInterceptors(registry);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
</div>
<link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-b6c3c6d139.css" rel="stylesheet">
<div class="more-toolbox">
<div class="left-toolbox">
<ul class="toolbox-list">
<li class="tool-item tool-active is-like "><a href="javascript:;"><svg class="icon" aria-hidden="true">
<use xlink:href="#csdnc-thumbsup"></use>
</svg><span class="name">点赞</span>
<span class="count"></span>
</a></li>
<li class="tool-item tool-active is-collection "><a href="javascript:;" data-report-click="{"mod":"popu_824"}"><svg class="icon" aria-hidden="true">
<use xlink:href="#icon-csdnc-Collection-G"></use>
</svg><span class="name">收藏</span></a></li>
<li class="tool-item tool-active is-share"><a href="javascript:;" data-report-click="{"mod":"1582594662_002"}"><svg class="icon" aria-hidden="true">
<use xlink:href="#icon-csdnc-fenxiang"></use>
</svg>分享</a></li>
<!--打赏开始-->
<!--打赏结束-->
<li class="tool-item tool-more">
<a>
<svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg>
</a>
<ul class="more-box">
<li class="item"><a class="article-report">文章举报</a></li>
</ul>
</li>
</ul>
</div>
</div>
<div class="person-messagebox">
<div class="left-message"><a href="https://blog.csdn.net/weixin_39168678">
<img src="https://profile.csdnimg.cn/0/E/6/3_weixin_39168678" class="avatar_pic" username="weixin_39168678">
<img src="https://g.csdnimg.cn/static/user-reg-year/2x/3.png" class="user-years">
</a></div>
<div class="middle-message">
<div class="title"><span class="tit"><a href="https://blog.csdn.net/weixin_39168678" data-report-click="{"mod":"popu_379"}" target="_blank">白马湖小龙王</a></span>
</div>
<div class="text"><span>发布了88 篇原创文章</span> · <span>获赞 39</span> · <span>访问量 17万+</span></div>
</div>
<div class="right-message">
<a href="https://im.csdn.net/im/main.html?userName=weixin_39168678" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-letter">私信
</a>
<a class="btn btn-sm bt-button personal-watch" data-report-click="{"mod":"popu_379"}">关注</a>
</div>
</div>
</div>
</article>