博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
解决 Springboot中Interceptor拦截器中依赖注入失败
阅读量:6658 次
发布时间:2019-06-25

本文共 1052 字,大约阅读时间需要 3 分钟。

问题:

在Springboot拦截器Interceptor中使用@Resource依赖注入时,发现运行的时候被注解的对象居然是null,没被注入进去

原配置为:

@Configuration public class WebAppConfig extends WebMvcConfigurerAdapter {
@Override public void addInterceptors(final InterceptorRegistry registry) {
registry.addInterceptor(new UserInterceptor()) .addPathPatterns("/**") .excludePathPatterns("/login/**", "/logout/**", "/loginPage/**", "/error/**"); super.addInterceptors(registry); } }

 

解决:

在Spring添加拦截器之前先自己创建一下这个Spring Bean,这样就能在Spring映射这个拦截器前,把拦截器中的依赖注入给完成了。

修改配置:

@Configuration public class WebAppConfig extends WebMvcConfigurerAdapter {
@Bean public UserInterceptor userInterceptor() {
return new UserInterceptor(); } @Override public void addInterceptors(final InterceptorRegistry registry) {
registry.addInterceptor(userInterceptor()) .addPathPatterns("/**") .excludePathPatterns("/login/**", "/logout/**", "/loginPage/**", "/error/**"); super.addInterceptors(registry); } }

 

转载于:https://www.cnblogs.com/niceboat/p/6958895.html

你可能感兴趣的文章
LitePal的使用
查看>>
查找旁站路径的几种方法
查看>>
Cisco路由配置入门
查看>>
我的友情链接
查看>>
创建并调用 DLL(1)
查看>>
lvs+keepalived实现DR模式热备
查看>>
各种媒体数据以 base64 编码方式直接嵌入网页中的写法
查看>>
由客户现场引发的思考
查看>>
Android应用及应用管理
查看>>
Xcode8 missing file 报出 ”xx“is missing from working copy 的问题 解决方法汇总
查看>>
rhsl 6.5 搭建DNS服务器
查看>>
推荐最适合IT人自学的6个视频网站、8个社区网站,欢迎补充
查看>>
idea配置tomcat
查看>>
自定义Iptables日志输出
查看>>
grep精确匹配(存在. -的情况)
查看>>
View Horizon Mirage安装手册(一)——Horizon Mirage介绍
查看>>
Mac OSX 正确地同时安装Python 2.7 和Python3
查看>>
python 爬虫之BeautifulSoup 库的基本使用
查看>>
Linux 文件查找命令详解
查看>>
MySQL中权限管理
查看>>