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 ...
随机推荐
- yii2.0表单自带验证码
Yii2.0的自带的验证依赖于GD2或者ImageMagick扩展. 使用步骤如下: 第一步,控制器: 在任意controller里面重写方法
- Task执行内幕与结果处理解密
本课主题 Task执行内幕与结果处理解密 引言 这一章我们主要关心的是 Task 是怎样被计算的以及结果是怎么被处理的 了解 Task 是怎样被计算的以及结果是怎么被处理的 Task 执行原理流程图 ...
- zt 设计模式六大原则(3):依赖倒置原则
下面说法对不对? 父类将算法(逻辑)封装起来,子类实现细节:这个就叫DIP(依赖倒置:Dependency Inversion Principles),模板模式就是这个原则的实现.如果在父类中加一个t ...
- 解决Ubuntu启动错误——kernel panic not syncing vfs unable to mount root fs on unknown-block 0 0 – error
最近在倒腾Ubuntu,然后想着怎么美化一下界面,于是照着网上的教程整了一下Flatabulous这个软件,然后好像/boot就满了.关机之后再开机就出现了如题所述的错误,无法开机,也无法进入reco ...
- thinkPHP输出sql语句(3.2和5.0通用)
//5.0$qwe = db::table('think_user')->where('id',1)->fetchsql()->column('name'); dump($qwe); ...
- Windows平台JxCore打包
1.下载JxCore https://raw.githubusercontent.com/jxcore/jxcore-release/master/0311/jx_win64v8.zip 2.解压并配 ...
- pat——1017. Queueing at Bank (java中Map用法)
由PAT1017例题展开: Suppose a bank has K windows open for service. There is a yellow line in front of the ...
- CodeForces - 616C(很有意思的bfs,set,map的使用)
传送门: http://codeforces.com/problemset/problem/616/C C. The Labyrinth time limit per test 1 second me ...
- 使用nuget 打包并上传 nuget.org
一. 准备工作 1 下载 Download NuGet.exe 2 windows 系统下设置环境变量 path中 或者 在dos 命令窗口下转到 nuget.exe 所在目录 3 在www.nu ...
- C# 5.0中使用CallerMemberName、CallerFilePath和CallerLineNumber获取代码的调用方信息(转载)
很多时候,我们需要在运行过程中记录一些调测的日志信息,如下所示: public void DoProcessing() { TraceMessage("DoProcessing()被XXX调 ...