过滤器、拦截器、AOP、ControllerAdvcie执行顺序对比
0. 执行顺序
过滤器 ➡ 拦截器 ➡ AOP ➡ ControllerAdvice ➡ Controller
没有异常的情况下,执行顺序如下:
有异常的情况下,执行顺序如下:
tip: 当产生异常后,无论是否有ControllerAdvice处理,HandlerInterceptor都不会执行
postHandle
方法, 直接执行afterCompletion
方法。
1. 过滤器
过滤器是Servlet规范中的一部分,它依赖于Servlet容器,因此只能在web程序中使用。过滤器可以拦截到客户端发送的请求和服务器响应的内容,对它们进行过滤处理,比如实现URL级别的权限控制、过滤敏感词汇、压缩响应信息等。
通过filterChain.doFilter(req, resp);
可以看出Filter是基于责任链模式的。
package com.licw.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.servlet.*;
import java.io.IOException;
@Slf4j
@Component
public class WebFilter implements Filter {
@Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain filterChain) throws IOException, ServletException {
log.info("filter: before");
filterChain.doFilter(req, resp);
log.info("filter: after");
}
}
2. 拦截器
拦截器是依赖于Spring框架的,因此只能在Spring MVC中使用。拦截器是基于代理实现的。
// 拦截器
package com.licw.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@SuppressWarnings("all")
@Slf4j
@Component
public class WebInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest req, HttpServletResponse resp, Object handler) throws Exception {
log.info("interceptor: before");
return true;
}
@Override
public void postHandle(HttpServletRequest req, HttpServletResponse resp, Object handler, ModelAndView modelAndView) throws Exception {
log.info("interceptor: after");
}
@Override
public void afterCompletion(HttpServletRequest req, HttpServletResponse resp, Object handler, Exception ex) throws Exception {
log.info("interceptor: after completion");
}
}
// 配置拦截器
package com.licw.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new WebInterceptor())
.addPathPatterns("/**");
}
}
3. AOP
AOP是面向切面编程,它可以在不修改源代码的情况下,通过动态代理的方式,在方法调用前后执行一些操作。AOP可以用于实现日志记录、事务管理、权限控制等功能。
// AOP
package com.licw.config;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
@Slf4j
@Aspect
@Component
public class WebAop {
@Around("execution(* com.licw.controller.BasicController.*(..))")
public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
log.info("aop: before");
try {
return proceedingJoinPoint.proceed();
} finally {
log.info("aop: after");
}
}
}
4. ControllerAdvice
ControllerAdvice是Spring MVC提供的一种全局异常处理机制,它可以定义在全局范围内处理Controller中的异常。ControllerAdvice是依赖于Spring MVC的,因此只能在Spring MVC中使用。
package com.licw.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@Slf4j
@RestControllerAdvice
public class ExceptionAdvice {
@ExceptionHandler(Exception.class)
public String allExceptionHandler(Exception e) {
log.info("RestControllerAdvice: {}", e.getMessage());
return "系统异常,请稍后再试";
}
}
标签:拦截器,AOP,springframework,org,ControllerAdvcie,import,public
From: https://www.cnblogs.com/licwuu/p/18352459