最愛Java

          書山有路勤為徑,學(xué)海無涯苦作舟

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

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

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


          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(
          "-----------------------------------------");
              }
              
              

          }



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

                  在cflow(Pointcut)的當(dāng)前實(shí)現(xiàn)中,在使用它時(shí),其實(shí)現(xiàn)方式會(huì)引入大量的系統(tǒng)開銷。在可能的地方,并且連接點(diǎn)重用不受影響時(shí),可以考慮優(yōu)先使用withincode(Signature)切入點(diǎn)。

              二.捕獲程序控制流程內(nèi)的所有連接點(diǎn),不包括初始連接點(diǎn)
              使用cflowbelow(Pointcut)切入點(diǎn)。cflowbelow(Pointcut)切入點(diǎn)的語法如下:
              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(
          "-----------------------------------------");
              }
              
              

          }



              這里和第一部分介紹的內(nèi)容有點(diǎn)區(qū)別;其區(qū)別是實(shí)際捕獲的連接點(diǎn)數(shù)量。cflow(Pointcut)切入點(diǎn)會(huì)觸發(fā)在初始連接點(diǎn)環(huán)境內(nèi)遇到的所有連接點(diǎn)(包括初始連接點(diǎn))上的通知,而cflowbelow(Pointcut)切入點(diǎn)則不包括那個(gè)初始連接點(diǎn)。

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

          公告


          導(dǎo)航

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

          統(tǒng)計(jì)

          常用鏈接

          留言簿(4)

          隨筆分類

          隨筆檔案

          收藏夾

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 汾西县| 即墨市| 绥滨县| 同仁县| 澄迈县| 秦安县| 惠水县| 柯坪县| 乳源| 宣化县| 沛县| 衡南县| 鲁山县| 绥滨县| 阿尔山市| 江都市| 玛沁县| 江陵县| 张家港市| 尖扎县| 闵行区| 昔阳县| 富民县| 平果县| 乌拉特前旗| 满洲里市| 凤翔县| 彰武县| 石狮市| 惠来县| 库尔勒市| 云霄县| 霍邱县| 桑植县| 慈利县| 吉安县| 大洼县| 长白| 武安市| 夏河县| 南木林县|