拦截器
-
创建拦截器
/** * @author wsyjlly * @create 2019.06.13 - 16:52 **/ public class FileUploadInterceptor implements HandlerInterceptor { private Logger logger = LoggerFactory.getLogger(getClass()); @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { logger.debug("FileUploadInterceptor...preHandle"); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { logger.debug("FileUploadInterceptor...postHandle"); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { logger.debug("FileUploadInterceptor...afterCompletion"); } }
-
注册拦截器
-
注册方式一:一般注册方式
/** * @author wsyjlly * @create 2019.06.12 - 16:17 **/ @Configuration public class WebMvcConfig implements WebMvcConfigurer { /* * 配置拦截器 * */ @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new FileUploadInterceptor()). addPathPatterns("/upload").order(1); } }
-
注册方式二:当拦截器中需要注入变量时须提前加载该拦截器
/** * @author wsyjlly * @create 2019.06.12 - 16:17 **/ @Configuration public class WebMvcConfig implements WebMvcConfigurer { /* * 配置拦截器 * */ @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(getFileUploadInterceptor()). addPathPatterns("/upload").order(1); } @Bean public FileUploadInterceptor getFileUploadInterceptor(){ return new FileUploadInterceptor(); } }
-