在Spring 的AOP中,如果一個Proxy同時實(shí)現(xiàn)MethodBeforeAdvice、AfterReturningAdvice和MethodInterceptor接口,那么這三個Advice的執(zhí)行順序是什么樣的呢?
經(jīng)過試驗(yàn),是和xml文件中的定義順序有關(guān)的。
如果Proxy的接口實(shí)現(xiàn)定義為
?????
?????????
??????????? MethodBeforeAdvice
??????????? AfterReturningAdvice
??????????? MethodInterceptor
?????????
?????
那么執(zhí)行的結(jié)果是
MethodBeforeAdvice
MethodInterceptor: before call
Really method excuting
MethodInterceptor: after call
AfterReturningAdvice
也就是說,執(zhí)行順序是:MethodBeforeAdvice,MethodInterceptor的調(diào)用前的部分,目標(biāo)方法,MethodInterceptor的調(diào)用后的部分,AfterReturningAdvice。
如果proxy的定義是
?????
?????????
??????????? MethodBeforeAdvice
??????????? MethodInterceptor
??????????? AfterReturningAdvice
?????????
?????
執(zhí)行的結(jié)果是
MethodBeforeAdvice
MethodInterceptor: before call
Really method excuting
AfterReturningAdvice
MethodInterceptor: after call
也就是說,執(zhí)行的順序是:MethodBeforeAdvice,MethodInterceptor的調(diào)用前的部分,目標(biāo)方法,AfterReturningAdvice,MethodInterceptor的調(diào)用后的部分。
如果proxy的定義是
?????
?????????
??????????? MethodInterceptor
??????????? MethodBeforeAdvice
??????????? AfterReturningAdvice
?????????
?????
執(zhí)行的結(jié)果是:
MethodInterceptor: before call
MethodBeforeAdvice
Really method excuting
AfterReturningAdvice
MethodInterceptor: after call
也就是說,執(zhí)行的順序是:MethodInterceptor的調(diào)用前的部分,MethodBeforeAdvice,目標(biāo)方法,AfterReturningAdvice,MethodInterceptor的調(diào)用后的部分。
以上的順序是在springframework 1.2.5中測試的。