配置拦截器:
package com.example.recorddemo.configuration;import com.example.recorddemo.interceptor.MyInterceptor;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.HandlerInterceptor;import org.springframework.web.servlet.config.annotation.InterceptorRegistration;import org.springframework.web.servlet.config.annotation.InterceptorRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;import javax.annotation.Resource;/** * @author wangchao */@Configurationpublic class InterceptorConfiguration implements WebMvcConfigurer {@ResourceMyInterceptor myInterceptor;/*** 添加拦截器* @param registry*/@Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(myInterceptor).addPathPatterns("/**");}}
registry.addInterceptor()
方法会返回当前的 interceptor
,因此可直接执行 addPathPatterns()
方法
public InterceptorRegistration addInterceptor(HandlerInterceptor interceptor) {InterceptorRegistration registration = new InterceptorRegistration(interceptor);this.registrations.add(registration);return registration;}
拦截器的执行顺序类似于栈,按照如下顺序执行:
preHandle-1, preHandle-2, postHandle-2, postHandle-1, afterCompletion-2, afterCompletion-1
推荐阅读
- 天玑1000和骁龙765哪个好_联发科天玑1000和骁龙765哪个好
- 二 Java多线程-线程关键字
- 当贝f3和极米h3坚果j10对比评测
- 怎样吸烟才能降低吸烟对身体的伤害(少吸烟和不吸烟的区别)
- ref 源码解析 Vue ref 和 v-for 结合
- 图文全面详解 Kafka 架构和原理机制
- 京东云开发者|关于“React 和 Vue 该用哪个”我真的栓Q
- SpringBoot 常用注解的原理和使用
- 荣耀50se和opporeno6对比_荣耀50se和opporeno6哪个好
- 华为nova9Pro和lQO0Z5区别对比_哪款更值得入手