过滤器,拦截器,Aspect 的区别

先来看一张图

 

这是它们的生命周期

当程序运行时,首先执行的的是过滤器的init的初始化方法,这是程序的运行时完成的

     这是程序还没执行完成就已经完成了

之后是过滤器的preHandle方法,紧跟着是ControllerAdvice,之后的Spring的面向切面Aspect方法,最后出场的是controller

当然这是初始化,相反如果controller抛出一个异常,顺序是相反的,先是Aspect捕获到然后是ControllerAdvice (这就是为什么写在ControllerAdvice里面的自定义捕获异常方法无法在过滤器和拦截器里面使用),接着是拦截器,最后才是过滤器。

他们的执行操作也是从外面到里面(Aspect是单独的一个方法,没有初始化和正式运行的结界,所以他的执行在其他之前)

他们的死亡和销毁是从面到外面(过滤器)

这是所有他们的生命周期,

 

filter is run 
preHandle
com.imooc.web.controller.UserController$$EnhancerBySpringCGLIB$$366bae69
public com.imooc.dto.User com.imooc.web.controller.UserController.getInfo(java.lang.String)
aspect is running
args1
进入GetInfo
time aspect 耗时:5
time aspect is finished
postHandle is run
耗时:87
afterHandle
耗时:88
exnull
end and runtime is102

 

posted @ 2019-06-19 12:32  JustRun1  阅读(262)  评论(0编辑  收藏  举报