java 三大拦截机制

本文详细介绍了Java中三种主要的拦截机制:Filter过滤器、Interceptor拦截器和AOP切面,包括它们的定义、实现方式及在Spring MVC框架中的应用。同时,探讨了不同拦截机制之间的执行顺序以及相同类型拦截器的执行流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

java 三大拦截机制

@RestController
public class MyController {

    @RequestMapping("/")
    public String hello(String message) throws ExecutionException, InterruptedException {
        System.err.println("********处理业务逻辑*********");
        return "hello";
    }
}

1.1 Filter 过滤器

@WebFilter(urlPatterns = "/*")
@Configuration  //或者@Component
public class MyFilter extends OncePerRequestFilter {

    protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
        System.err.println("-----filter开始----");
        filterChain.doFilter(httpServletRequest, httpServletResponse);
        System.err.println("-----filter结束------");
    }
}
-----filter开始----
********处理业务逻辑*********
-----filter结束------

1.2 Interceptor 拦截器

1.2.1 声明拦截器

//或者 继承  HandlerInterceptorAdapter  (适配器模式)
public class MyInterceptor implements HandlerInterceptor{

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.err.println("preHandle");
        return true;
    }

    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.err.println("postHandle");
    }

    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.err.println("afterCompletion");
    }
}

1.2.3 加入springMVC

@Configuration
public class MvcConfig extends WebMvcConfigurationSupport {

    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new MyInterceptor());
    }
}
preHandle
********处理业务逻辑*********
postHandle
afterCompletion

1.3 切面

SpringAop 详解 https://blog.csdn.net/weixin_42835389/article/details/83662354

@Aspect
public class MyAspectj {

    @Pointcut("within(com.example.MyController)") //匹配MyController包下的所有类的所有方法
    private void pointCut_within() {

    }

    @Around("pointCut_within()")
    public Object afterThrowing(ProceedingJoinPoint joinPoint) throws Throwable {
        // 方法执行前
        System.err.println("-------aop before-------");
        Object retVal = joinPoint.proceed();
        // 方法执行后
        System.err.println("-------aop after-------");
        return retVal;
    }
}
-------aop before-------
********处理业务逻辑*********
-------aop after-------

2.1 不同拦截机制 执行顺序

在这里插入图片描述

-----filter开始----
----interceptor--------------preHandle
-------aop before-------
********处理业务逻辑*********
-------aop after-------
----interceptor--------------preHandle
----interceptor--------------preHandle
-----filter结束------

2.2 相同拦截机制 执行顺序

过滤器 和 Aop 都可以用 @Order 来指定 过来顺序
拦截器则 通过加入SpringMVC的先后顺序来执行
@Order() 默认为 @Order(Integer.MAX_VALUE) 数字越小 越先执行

@WebFilter(urlPatterns = "/*")
@Configuration
@Order(1)
public class MyFilter extends OncePerRequestFilter {

    protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
        System.err.println("-----filter开始----");
        filterChain.doFilter(httpServletRequest, httpServletResponse);
        System.err.println("-----filter结束------");
    }
}
@WebFilter(urlPatterns = "/*")
@Configuration
@Order(0)
public class MyFilter2 extends OncePerRequestFilter {

    protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
        System.err.println("-----filter 2开始----");
        filterChain.doFilter(httpServletRequest, httpServletResponse);
        System.err.println("-----filter 2结束------");
    }
}
-----filter 2开始----
-----filter开始----
********处理业务逻辑*********
-----filter结束------
-----filter 2结束------
@Aspect
@Configuration
@Order(1)
public class MyAspectj {

    @Pointcut("within(com.example.MyController)") //匹配service包下的所有类的所有方法
    private void pointCut_within() {

    }

    @Around("pointCut_within()")
    public Object afterThrowing(ProceedingJoinPoint joinPoint) throws Throwable {
        // 方法执行前
        System.err.println("-------aop before-------");
        Object retVal = joinPoint.proceed();
        // 方法执行后
        System.err.println("-------aop after-------");
        return retVal;
    }
}
@Aspect
@Configuration
@Order(0)
public class MyAspectj2 {

    @Pointcut("within(com.example.MyController)") //匹配service包下的所有类的所有方法
    private void pointCut_within() {

    }

    @Around("pointCut_within()")
    public Object afterThrowing(ProceedingJoinPoint joinPoint) throws Throwable {
        // 方法执行前
        System.err.println("-------aop2 before-------");
        Object retVal = joinPoint.proceed();
        // 方法执行后
        System.err.println("-------aop2 after-------");
        return retVal;
    }
}
-------aop2 before-------
-------aop before-------
********处理业务逻辑*********
-------aop after-------
-------aop2 after-------
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值