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 ...
随机推荐
- 475. Heaters (start binary search, appplication for binary search)
Winter is coming! Your first job during the contest is to design a standard heater with fixed warm r ...
- Hibernate映射Map属性
看到一篇Hibernate 映射Map属性的文章挺好的转载一下原地址:http://blog.sina.com.cn/s/blog_86f4502c0101fs1x.html Map集合属于有序集合, ...
- Django 查询集的过滤内置条件
条件选取querySet的时候,filter表示=,exclude表示!=.querySet.distinct() 去重复__exact 精确等于 like 'aaa' __iexact 精确等于 忽 ...
- CAShapeLayer使用
UIView *showView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)]; [self.view addSubv ...
- CentOS下go 安装
go 语言源码安装依赖 ,gcc ,make glibc库,等,上述工具安装省略,另外,其源代码更新采用的是mercurial 工具,安装前先安装mercureal : 1.mercurial安 ...
- 使用appium在android9.0真机上测试程序时报错command failed shell “ps ‘uiautomator’”的解决办法
appium目前最新的windows版本是1.4.16,在android9.0真机上测试程序时会报错:command failed shell “ps ‘uiautomator’”. 网上大多数人的解 ...
- 2019.1.1 在重写的方法上面添加@Override注解方法报错解决办法
报错代码 The method transfer(Integer, Integer, Double) of type AccountServiceImpl must override a superc ...
- DataTables.Queryable Sample
1.DataTables.Queryable的例子项目使用了SQL Server CE数据库,花了几分钟时间转为使用LocalDB. 完整Web.config文件如下: <?xml versio ...
- tensorflow一个很好的博客
http://blog.csdn.net/mydear_11000/article/details/53197891
- 安装JDK8
安装JDK8 1.去http://www.Oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html中下载JDK的 ...