Springboot 拦截器(HandlerInterceptorAdapter)中注入无效
1,传统filter和HandlerInterceptorAdapter的区别
在preHandle(预处理)中,可以进行编码、安全控制等处理;
在postHandle(后处理)中,有机会修改ModelAndView;
在afterCompletion(返回处理)中,可以根据ex是否为null判断是否发生了异常,进行日志记录。
总之,传统的filter可以完成的功能,HandlerInterceptorAdapter都以完成。更详细信息可以查看HandlerInterceptorAdapter源码。
2,HandlerInterceptorAdapter的子类中,注入无效问题。
正确的步骤如下:
2.1,写一个类,继承HandlerInterceptorAdapter(抽象类),并重写响应的方法。
@SuppressWarnings("ALL")
@Component
public class GlobalInterceptor extends HandlerInterceptorAdapter {
@Autowired
ReportLogEntityMapper logService;
private long start = System.currentTimeMillis();
@Override
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse response, Object handler) throws Exception {
start = System.currentTimeMillis();
return super.preHandle(httpServletRequest, response, handler);
}
//存储查询消耗时间,以后优化代码时查询
@Override
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
ReportLogEntity logEntity = new ReportLogEntity();
logEntity.setCostTime((System.currentTimeMillis() - start));
logEntity.setRequestUrl(new String(httpServletRequest.getRequestURL()));
logEntity.setRequestUri(httpServletRequest.getRequestURI());
logEntity.setQueryString(httpServletRequest.getQueryString());
logEntity.setRemoteAddr(httpServletRequest.getRemoteAddr());
logEntity.setCreatedDate(new Date());
logService.insertSelective(logEntity);
}
2.2,将该类在启动的时候,通过注解(@Component)交给spring托管,
2.3,在WebMvcConfigurerAdapter类的子类中的
@Configuration
public class InterceptorConfig extends WebMvcConfigurerAdapter {
@Autowired
private GlobalInterceptor globalInterceptor; public static void main(String[] args) {
SpringApplication.run(SuperrescueReportingApplication.class, args);
} @Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new ReportInterceptor()).addPathPatterns("/**");
registry.addInterceptor(globalInterceptor);
super.addInterceptors(registry);
}
}
注册即可。
上面主要做的事情就是,1,继承HandlerInterceptorAdapter,2,继承WebMvcConfigurerAdapter并注册拦截器,这里注册的时候,HandlerInterceptorAdapter子类必须是交给spring托管后的子类。
装载自:https://www.jianshu.com/p/33a69534ea08
Springboot 拦截器(HandlerInterceptorAdapter)中注入无效的更多相关文章
- SpringBoot拦截器中Bean无法注入(转)
问题 这两天遇到SpringBoot拦截器中Bean无法注入问题.下面介绍我的思考过程和解决过程: 1.由于其他bean在service,controller层注入一点问题也没有,开始根本没意识到Be ...
- SpringBoot拦截器中service或者redis注入为空的问题
原文:https://my.oschina.net/u/1790105/blog/1490098 这两天遇到SpringBoot拦截器中Bean无法注入问题.下面介绍我的思考过程和解决过程: 1.由于 ...
- SpringBoot拦截器中无法注入bean的解决方法
SpringBoot拦截器中无法注入bean的解决方法 在使用springboot的拦截器时,有时候希望在拦截器中注入bean方便使用 但是如果直接注入会发现无法注入而报空指针异常 解决方法: 在注册 ...
- springboot拦截器注入service为空
一般都是因为除了在拦截器之外,还需要在拦截器的配置类中,注册拦截器时没有使用spring的bean,而是使用了new创建bean造成的. @Configuration public class Web ...
- springboot + 拦截器 + 注解 实现自定义权限验证
springboot + 拦截器 + 注解 实现自定义权限验证最近用到一种前端模板技术:jtwig,在权限控制上没有用springSecurity.因此用拦截器和注解结合实现了权限控制. 1.1 定义 ...
- Springboot拦截器使用及其底层源码剖析
博主最近看了一下公司刚刚开发的微服务,准备入手从基本的过滤器以及拦截器开始剖析,以及在帮同学们分析一下上次的jetty过滤器源码与本次Springboot中tomcat中过滤器的区别.正题开始,拦截器 ...
- springboot拦截器总结
Springboot 拦截器总结 拦截器大体分为两类 : handlerInterceptor 和 methodInterceptor 而methodInterceptor 又有XML 配置方法 和A ...
- SpringMVC利用拦截器防止SQL注入
引言 随着互联网的发展,人们在享受互联网带来的便捷的服务的时候,也面临着个人的隐私泄漏的问题.小到一个拥有用户系统的小型论坛,大到各个大型的银行机构,互联网安全问题都显得格外重要.而这些网站的背后,则 ...
- Java结合SpringBoot拦截器实现简单的登录认证模块
Java结合SpringBoot拦截器实现简单的登录认证模块 之前在做项目时需要实现一个简单的登录认证的功能,就寻思着使用Spring Boot的拦截器来实现,在此记录一下我的整个实现过程,源码见文章 ...
- Springboot 拦截器的背后
今天写了个拦截器对一些mapping做了些处理,写完之后突然很想看看拦截器是怎么加进spring里面.对着源码debug了一遍.又有了新的收获. 1.拦截器的实现 1.实现HandlerInterce ...
随机推荐
- zt C++ list 类学习笔记
C++ list 类学习笔记 分类: C++ 2011-09-29 00:12 7819人阅读 评论(0) 收藏 举报 listc++iteratorvectorcconstructor 双向循环链表 ...
- 一个SAP开发人员的2018年终总结
我是SAP成都研究院的Jerry Wang,我喂自己袋盐. 时间过得真快,2017年发生的事情还历历在目,一转眼,2018年又马上要结束了. Jerry惊恐地发现,随着年龄的增长,时光流逝的速度仿佛有 ...
- Windows文件批量重命名
选择要命名的文件 按F2,编辑名字 然后按回车就行了 电视剧命名,我认为这样足够了
- HashMap 和 ConcurrentHashMap,Java1.8版本
1. HashMap Entry,一对kv就是一个Entry,还包括一些next指针,用来解决散列冲突. table,内部用来存储Entry的数组,resize时候table会成倍扩容. 容量,tab ...
- AWESOME SWIFT-swift.libhunt.com-swift类库网站
https://swift.libhunt.com/categories/688-events 29 Events libraries and projects ORDERED BY POPULARI ...
- UVa 1395 - Slim Span(最小生成树变形)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- 【dp】最大乘积
题目描述] 今年是国际数学联盟确定的“2000——世界数学年”,又恰逢我国著名数学家华罗庚先生诞辰90周年.在华罗庚先生的家乡江苏金坛,组织了一场别开生面的数学智力竞赛的活动,你的一个好朋友XZ也有幸 ...
- Jmeter--thrift接口压测
1. 安装thrift 2. 新建maven工程,代码结构如下 3. pom设置,按配置存放thrift文件和打包描述文件(具体代码见附件,根据需要改变配置信息) 4. thrift需要手动添加nam ...
- [转载] iOS应用程序的生命周期
iOS应用程序的生命周期 2015-06-23 iOS大全 (点击上方蓝字,快速关注我们) iOS应用程序一般都是由自己编写的代码和系统框架(system frameworks)组成,系统框架提供一些 ...
- Java日期类题目
每类题都有各种各样解决的方式,大家随意发散 分析以下需求,并用代码实现 1.已知日期字符串:"2015-10-20",将该日期字符串转换为日期对象 2.将(1)中的日期对象转换为日 ...