最愛Java

          書山有路勤為徑,學海無涯苦作舟

          《AspectJ Cookbook》讀書筆記十: 捕獲基于控制流程的連接點

              本章中描述的切入點支持捕獲另一個初始連接點作用域或環境內的所有連接點。每個連接點在程序的控制流程中都有一個具體位置,這為通過這里描述的切入點聲明捕獲的連接點提供了環境。
              一. 捕獲通過初始連接點開始的程序控制流程內的所有連接點
              使用cflow(Pointcut)切入點。cflow(Pointcut)切入點的語法如下:
              pointcut <pointcut name>(<any values to be picked up>) : cflow(<pointcut>);

              cflow(Pointcut)切入點具有3個關鍵特征:
                  1.cflow(Pointcut)切入點捕獲在初始特定的連接點環境內遇到的所有連接點,這個初始連接點是通過另一個切入點選擇的。
                  2.捕獲的連接點包括初始連接點。
                  3.作用域是cflow(pointcut)切入點中重要的鑒別器。這個切入點將捕獲通過切入點參數捕獲的連接點的控制流程內的所有連接點。


          package com.aspectj;

          public aspect CFlowRecipe {
              
          /**
               * Specifies calling advice whenever a method
               * matching the following rules gets called:
               * 
               * Class Name:MyClass
               * Method Name:foo
               * Method Return Type:void
               * Method Parameters: an int followed by a String
               
          */

              pointcut callInitialPointcut():call(
          void MyClass.foo(int,String));
              
              
          /**
               * Specifies calling advice whenever a join point is encountered
               * including and after the initial join point triggers the pointcut
               * that is specified in the parameter:
               * 
               * Pointcut Name:callInitialPointcut
               
          */

              pointcut cflowPointcut():cflow(callInitialPointcut());
              
              
          //Advice declaration
              before() : cflowPointcut() && !within(CFlowRecipe+{
                  System.out.println(
          "---------- Aspect Advice Logic ----------");
                  System.out.println(
          "In the advice picked by CFlowRecipe()");
                  System.out.println(
          "Join Point Kind: " + thisJoinPoint.getKind());
                  System.out.println(
          "Signature: " + thisJoinPoint.getStaticPart().getSignature());
                  System.out.println(
          "Source Line: " + thisJoinPoint.getStaticPart().getSourceLocation());
                  System.out.println(
          "-----------------------------------------");
              }
              
              

          }



                  值得更詳細研究cflow(Pointcut)做什么。這個特殊的切入點引入了連接點環境的概念。它是指每個連接點具有一個作用域,在這個用途域內,它被看成是執行程序的控制流程的一部分。
                  在這個控制流程內,任何遇到的連接點都會觸發cflow(Pointcut)切入點,并調用任何關聯的通知。當初始連接點觸發指定的切入點參數時,cflow(Pointcut)切入點會起作用,并觸發其關聯的通知。然后,將為在初始連接點環境內的控制流程中遇到的每個連接點調用與cflow(Pointcut)關聯的通知。最后,捕獲的連接點集合包括初始連接點本身,這就是這個切入點與cflowbelow(Pointcut)切入點之間的主要區別。

                  在cflow(Pointcut)的當前實現中,在使用它時,其實現方式會引入大量的系統開銷。在可能的地方,并且連接點重用不受影響時,可以考慮優先使用withincode(Signature)切入點。

              二.捕獲程序控制流程內的所有連接點,不包括初始連接點
              使用cflowbelow(Pointcut)切入點。cflowbelow(Pointcut)切入點的語法如下:
              pointcut <pointcut name>(<any values to be picked up>) : cflowbelow(<pointcut>);


          package com.aspectj;

          public aspect CFlowBelowRecipe {
              
          /**
               * Specifies calling advice whenever a method
               * matching the following rules gets called:
               * 
               * Class Name:MyClass
               * Method Name:foo
               * Method Return Type:void
               * Method Parameters: an int followed by a String
               
          */

              pointcut callInitialPointcut():call(
          void MyClass.foo(int,String));
              
              
          /**
               * Specifies calling advice whenever a join point is encountered
               * after the initial join point triggers the pointcut
               * that is specified in the parameter:
               * 
               * Pointcut Name:callInitialPointcut
               
          */

              pointcut cflowPointcut():cflowbelow(callInitialPointcut());
              
              
          //Advice declaration
              before() : cflowPointcut() && !within(CFlowBelowRecipe+{
                  System.out.println(
          "---------- Aspect Advice Logic ----------");
                  System.out.println(
          "In the advice picked by CFlowBelowRecipe()");
                  System.out.println(
          "Join Point Kind: " + thisJoinPoint.getKind());
                  System.out.println(
          "Signature: " + thisJoinPoint.getStaticPart().getSignature());
                  System.out.println(
          "Source Line: " + thisJoinPoint.getStaticPart().getSourceLocation());
                  System.out.println(
          "-----------------------------------------");
              }
              
              

          }



              這里和第一部分介紹的內容有點區別;其區別是實際捕獲的連接點數量。cflow(Pointcut)切入點會觸發在初始連接點環境內遇到的所有連接點(包括初始連接點)上的通知,而cflowbelow(Pointcut)切入點則不包括那個初始連接點。

          posted on 2008-08-25 10:36 Brian 閱讀(422) 評論(0)  編輯  收藏 所屬分類: 《AspectJ Cookbook》讀書筆記

          公告


          導航

          <2008年8月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456

          統計

          常用鏈接

          留言簿(4)

          隨筆分類

          隨筆檔案

          收藏夾

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 苍溪县| 陇南市| 阿勒泰市| 辉南县| 焦作市| 西林县| 金溪县| 丹江口市| 台州市| 阿荣旗| 苗栗市| 廉江市| 阳信县| 张家川| 邹平县| 马边| 临朐县| 汽车| 南漳县| 镇康县| 宁波市| 曲阜市| 隆子县| 静宁县| 永和县| 朔州市| 滁州市| 巴彦县| 建始县| 望江县| 榆树市| 东城区| 汉中市| 景洪市| 雷波县| 龙游县| 财经| 韶关市| 太谷县| 堆龙德庆县| 临安市|