1,传统filter和HandlerInterceptorAdapter的区别

springboot对传统Filter进行增强,添加更多细粒度的操作,分别实现预处理、后处理(调用了Service并返回ModelAndView,但未进行页面渲染)、返回处理(已经渲染了页面)
在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)中注入无效的更多相关文章

  1. SpringBoot拦截器中Bean无法注入(转)

    问题 这两天遇到SpringBoot拦截器中Bean无法注入问题.下面介绍我的思考过程和解决过程: 1.由于其他bean在service,controller层注入一点问题也没有,开始根本没意识到Be ...

  2. SpringBoot拦截器中service或者redis注入为空的问题

    原文:https://my.oschina.net/u/1790105/blog/1490098 这两天遇到SpringBoot拦截器中Bean无法注入问题.下面介绍我的思考过程和解决过程: 1.由于 ...

  3. SpringBoot拦截器中无法注入bean的解决方法

    SpringBoot拦截器中无法注入bean的解决方法 在使用springboot的拦截器时,有时候希望在拦截器中注入bean方便使用 但是如果直接注入会发现无法注入而报空指针异常 解决方法: 在注册 ...

  4. springboot拦截器注入service为空

    一般都是因为除了在拦截器之外,还需要在拦截器的配置类中,注册拦截器时没有使用spring的bean,而是使用了new创建bean造成的. @Configuration public class Web ...

  5. springboot + 拦截器 + 注解 实现自定义权限验证

    springboot + 拦截器 + 注解 实现自定义权限验证最近用到一种前端模板技术:jtwig,在权限控制上没有用springSecurity.因此用拦截器和注解结合实现了权限控制. 1.1 定义 ...

  6. Springboot拦截器使用及其底层源码剖析

    博主最近看了一下公司刚刚开发的微服务,准备入手从基本的过滤器以及拦截器开始剖析,以及在帮同学们分析一下上次的jetty过滤器源码与本次Springboot中tomcat中过滤器的区别.正题开始,拦截器 ...

  7. springboot拦截器总结

    Springboot 拦截器总结 拦截器大体分为两类 : handlerInterceptor 和 methodInterceptor 而methodInterceptor 又有XML 配置方法 和A ...

  8. SpringMVC利用拦截器防止SQL注入

    引言 随着互联网的发展,人们在享受互联网带来的便捷的服务的时候,也面临着个人的隐私泄漏的问题.小到一个拥有用户系统的小型论坛,大到各个大型的银行机构,互联网安全问题都显得格外重要.而这些网站的背后,则 ...

  9. Java结合SpringBoot拦截器实现简单的登录认证模块

    Java结合SpringBoot拦截器实现简单的登录认证模块 之前在做项目时需要实现一个简单的登录认证的功能,就寻思着使用Spring Boot的拦截器来实现,在此记录一下我的整个实现过程,源码见文章 ...

  10. Springboot 拦截器的背后

    今天写了个拦截器对一些mapping做了些处理,写完之后突然很想看看拦截器是怎么加进spring里面.对着源码debug了一遍.又有了新的收获. 1.拦截器的实现 1.实现HandlerInterce ...

随机推荐

  1. 第四周 day4 python学习笔记

    关于装饰器的更多信息可以参考http://egon09.blog.51cto.com/9161406/1836763 1.装饰器Decorator 装饰器:本质上是函数,(装饰其他函数),就是为其他函 ...

  2. PLSQL使用绑定变量

    想对一个sql做10046trace,结果因为10g数据库无法对sql_id做,只能使用绑定变量的方法,下面sql是如何使用绑定变量运行sql的语句 declare  v_sql  VARCHAR2( ...

  3. 迷宫问题——BFS

    改进版 BFS #include <bits/stdc++.h> using namespace std; #define coordi(x,y) ( m*(x-1)+y ) const ...

  4. 通过ajax获取一个多位数,当容器显示在屏幕可视区时,让数字以滚动的形式显示

      { "data": "268" } json数据 <!DOCTYPE html> <html> <head lang=&quo ...

  5. MATLAB入门学习(五)

    现在,我们来学画图吧.╭( ・ㅂ・)و ̑̑ 绘制函数图像最常用的命令是plot plot(x,y,s)x,y为同维向量,绘制分别以x为横坐标,y为纵坐标的曲线 如果x y 是矩阵的话则会绘制多条曲线 ...

  6. 当你的Spring IOC 容器(即applicationContext.xml文件)忘记配到web.xml 文件中时

    当你的Spring IOC 容器忘记配到web.xml 文件中时,启动服务器就会报错. 部分错误如下: Caused by: org.springframework.beans.factory.NoS ...

  7. 一对一关联关系基于主键映射的异常 IdentifierGenerationException

    具体异常:org.hibernate.id.IdentifierGenerationException: attempted to assign id from null one-to-one pro ...

  8. Avito Cool Challenge 2018 E. Missing Numbers 【枚举】

    传送门:http://codeforces.com/contest/1081/problem/E E. Missing Numbers time limit per test 2 seconds me ...

  9. Search - Dictionary

    Search III Your task is to write a program of a simple dictionary which implements the following ins ...

  10. 使用Nginx 做负载均衡

    Nginx可以作为一个非常高效的负载均衡系统,通过分发HTTP请求到多个应用服务器来提高整个系统的吞吐量,性能和可用性. 负载均衡的算法/机制 下面是Nginx支持的机制 轮询机制 轮询算法 最少连接 ...