`

Spring AOP中MethodBeforeAdvice、AfterReturningAdvice

阅读更多
Spring AOP中MethodBeforeAdvice、AfterReturningAdvice和MethodInterceptor的执行顺序
在Spring 的AOP中,如果一个Proxy同时实现MethodBeforeAdvice、AfterReturningAdvice和MethodInterceptor接口,那么这三个Advice的执行顺序是什么样的呢?
经过试验,是和xml文件中的定义顺序有关的。
如果Proxy的接口实现定义为
     
         
            MethodBeforeAdvice
            AfterReturningAdvice
            MethodInterceptor
         
     
那么执行的结果是
MethodBeforeAdvice

MethodInterceptor: before call

Really method excuting

MethodInterceptor: after call

AfterReturningAdvice

也就是说,执行顺序是:MethodBeforeAdvice,MethodInterceptor的调用前的部分,目标方法,MethodInterceptor的调用后的部分,AfterReturningAdvice。

如果proxy的定义是
     
         
            MethodBeforeAdvice
            MethodInterceptor
            AfterReturningAdvice
         
     
执行的结果是
MethodBeforeAdvice

MethodInterceptor: before call

Really method excuting

AfterReturningAdvice

MethodInterceptor: after call
也就是说,执行的顺序是:MethodBeforeAdvice,MethodInterceptor的调用前的部分,目标方法,AfterReturningAdvice,MethodInterceptor的调用后的部分。

如果proxy的定义是
     
         
            MethodInterceptor
            MethodBeforeAdvice
            AfterReturningAdvice
         
     
执行的结果是:
MethodInterceptor: before call

MethodBeforeAdvice

Really method excuting

AfterReturningAdvice

MethodInterceptor: after call
也就是说,执行的顺序是:MethodInterceptor的调用前的部分,MethodBeforeAdvice,目标方法,AfterReturningAdvice,MethodInterceptor的调用后的部分。
以上的顺序是在springframework 1.2.5中测试的。


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics