在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中測試的。