最愛Java

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

          《AspectJ Cookbook》讀書筆記七: 捕獲類和對象構(gòu)造上的連接點(diǎn)

          一.  捕獲對構(gòu)造函數(shù)的調(diào)用
              使用Call(Signature)寫入點(diǎn),它帶有額外的new關(guān)鍵字作為簽名的一部分。使用與構(gòu)造函數(shù)有關(guān)的call(Signature)切入點(diǎn)的語法如下:
              pointcut <pointcut name>(<any values to be picked up>): call(<optional modifier> <class>.new(<parameter types>));

              在用于捕獲對構(gòu)造函數(shù)的調(diào)用時(shí),call(Signature)切入點(diǎn)具有3個(gè)關(guān)鍵特征:
              1.在把一個(gè)類實(shí)例化成一個(gè)對象時(shí),具有new關(guān)鍵字的call(Signature)切入點(diǎn)會捕獲連接點(diǎn)。
              2.通過使用around()形式的通知,call(Signature)寫入點(diǎn)可以在Java的正常繼承規(guī)則的限制下重寫返回對象的類型。
              3.編譯器不會檢查指定的Signature是否對應(yīng)于實(shí)際的構(gòu)造函數(shù)。

           

          package com.aspectj;

          public aspect CallNewRecipe {
              
          /*
               * Specifies calling advice when any constructor is called
               * that meets the following signature rules:
               * 
               * Class Name:MyClass
               * Method Name:new (This is a keyword indicating the constructor call)
               * Method Parameters: int , String
               
          */

              pointcut myClassConstructorWithIntAndStringPointcut() : call(MyClass.
          new(int , String));
              
              
          //Advice declaration
              before() : myClassConstructorWithIntAndStringPointcut() {
                  System.out.println(
          "---------- Aspect Advice Logic ----------");
                  System.out.println(
          "In the advice picked by myClassConstructorWithIntAndOthersPointcut()");
                  System.out.println(
          "The current type of object under construction is:");
                  System.out.println(thisJoinPoint.getThis());
                  System.out.println(
          "Signature: " + thisJoinPoint.getSignature());
                  System.out.println(
          "Source Line: " + thisJoinPoint.getSourceLocation());
                  System.out.println(
          "-----------------------------------------");
              }

          }



          二. 在執(zhí)行構(gòu)造函數(shù)時(shí)捕獲它
              使用execution(Signature)切入點(diǎn),它帶有額外的new關(guān)鍵字作為簽名的一部分。使用與構(gòu)造函數(shù)有關(guān)的execution(Signature)切入點(diǎn)的語法如下:
              pointcut <pointcut name>(<any values to be picked up>):execution(<optional modifier> <class>.new<parameter types>));

              在用于捕獲構(gòu)造函數(shù)的執(zhí)行時(shí),execution(Signature)切入點(diǎn)具有3個(gè)關(guān)鍵特征:
              1.在執(zhí)行類的構(gòu)造函數(shù)時(shí),具有new關(guān)鍵字的execution(Signature)切入點(diǎn)會觸發(fā)連接點(diǎn)。
              2.不能在調(diào)用類的構(gòu)造函數(shù)之前那一刻觸發(fā)連接點(diǎn)。這會阻止重寫返回的對象。
              3.可以使用around()通知來重寫構(gòu)造函數(shù)方法的實(shí)現(xiàn),當(dāng)不能重寫正在構(gòu)造的對象的類型。


          package com.aspectj;

          public aspect ExecutionNewRecipe {
              
          /*
               * Specifies calling advice when any constructor executes
               * that meets the following signature rules:
               * 
               * Class Name:MyClass
               * Method Name:new (This is a keyword indicating the constructor call)
               * Method Parameters: int , String
               
          */

              pointcut myClassConstructorWithIntAndStringPointcut() : execution(MyClass.
          new(int,String));
              
              
          //Advice declaration
              before() : myClassConstructorWithIntAndStringPointcut() {
                  System.out.println(
          "---------- Aspect Advice Logic ----------");
                  System.out.println(
          "In the advice picked by myClassConstructorWithIntAndOthersPointcut()");
                  System.out.println(
          "The current type of object under construction is:");
                  System.out.println(thisJoinPoint.getThis().getClass());
                  System.out.println(
          "Signature: " + thisJoinPoint.getSignature());
                  System.out.println(
          "Source Line: " + thisJoinPoint.getSourceLocation());
                  System.out.println(
          "-----------------------------------------");        
              }

          }


          三. 捕獲何時(shí)初始化對象
              使用initialization(Signature)切入點(diǎn)。initialization(Signature)切入點(diǎn)的語法如下:
              pointcut <pointcut name>(<any values to be picked up>):initialization(<optional modifier> <class>.new<parameter types>));

              initialization(Signature)切入點(diǎn)具有5個(gè)關(guān)鍵特征:
              1.initialization(Signature)切入點(diǎn)必須包含new關(guān)鍵字。
              2.initialization(Signature)切入點(diǎn)捕獲連接點(diǎn)發(fā)生在任何超類的初始化之后,以及從構(gòu)造函數(shù)方法返回之前。
              3.Signature必須解析成特定類的構(gòu)造函數(shù),而不是一個(gè)簡單的方法。
              4.initialization(Signature)切入點(diǎn)提供了編譯時(shí)的檢查,用于檢查構(gòu)造函數(shù)是否正在被引用。
              5.由于AspectJ編譯器中的編譯器限制,當(dāng)與around()通知關(guān)聯(lián)時(shí),不能使用initialization(Signature)切入點(diǎn)。

           

          package com.aspectj;


          public aspect InitializationRecipe {
              
          /*
               * Specifies calling advice when any object
               * initializes using a constructor
               * that meets the following signature rules:
               * 
               * Class Name:MyClass
               * Method Name:new (This is a keyword indicating the constructor call)
               * Method Parameters: int and any others
               
          */

              pointcut myClassObjectInitializationWithIntAndStringPointcut() : execution(MyClass.
          new(int,*));
              
              
          //Advice declaration
              before() : myClassObjectInitializationWithIntAndStringPointcut() {
                  System.out.println(
          "---------- Aspect Advice Logic ----------");
                  System.out.println(
          "In the advice picked by myClassObjectInitializationWithIntAndStringPointcut()");
                  System.out.println(
          "The current type of object under construction is:");
                  System.out.println(thisJoinPoint.getThis().getClass());
                  System.out.println(
          "Signature: " + thisJoinPoint.getSignature());
                  System.out.println(
          "Source Line: " + thisJoinPoint.getSourceLocation());
                  System.out.println(
          "-----------------------------------------");        
              }

          }



          四. 捕獲何時(shí)將要初始化一個(gè)對象
              使用preinitialization(Signature)切入點(diǎn)。preinitialization(Signature)切入點(diǎn)的語法如下:
              pointcut <pointcut name>(<any values to be picked up>):preinitialization(<optional modifier> <class>.new<parameter types>));

              preinitialization(Signature)切入點(diǎn)具有5個(gè)關(guān)鍵特征:
              1.preinitialization(Signature)切入點(diǎn)必須包含new關(guān)鍵字。
              2.preinitialization(Signature)切入點(diǎn)捕獲連接點(diǎn)發(fā)生在進(jìn)入捕獲構(gòu)造函數(shù)之后,以及調(diào)用任何超類構(gòu)造函數(shù)之前。
              3.Signature必須解析成一個(gè)構(gòu)造函數(shù)。
              4.preinitialization(Signature)切入點(diǎn)提供了編譯時(shí)的檢查,用于檢查構(gòu)造函數(shù)是否正在被引用。
              5.由于AspectJ編譯器中的編譯器限制,當(dāng)與around()通知關(guān)聯(lián)時(shí),不能使用preinitialization(Signature)切入點(diǎn)。

           

          package com.aspectj;

          public aspect PreInitializationRecipe {
              
          /*
               * Specifies calling advice just before an object initializes
               * using a constructor that meets the following signature rules:
               * 
               * Class Name:MyClass
               * Method Name:new (This is a keyword indicating the constructor call)
               * Method Parameters: an int followed by a String
               
          */

              pointcut myClassIntStringObjectPreInitializationPointcut() : preinitialization(MyClass.
          new(int,String));
              
              
          //Advice declaration
              before(int number , String name) : myClassIntStringObjectPreInitializationPointcut() && args(number , name) {
                  System.out.println(
          "---------- Aspect Advice Logic ----------");
                  System.out.println(
          "In the advice picked by anyMyClassObjectInitializationPointcut()");
                  System.out.println(
          "The current type of object under construction is:");
                  System.out.println(thisJoinPoint.getThis());
                  System.out.println(
          "The values passed in where: " + number + " , " + name);
                  System.out.println(
          "Signature: " + thisJoinPoint.getSignature());
                  System.out.println(
          "Source Line: " + thisJoinPoint.getSourceLocation());
                  System.out.println(
          "-----------------------------------------");        
              }

          }



          五. 捕獲何時(shí)初始化類
              使用staticinitialization(TypePattern)寫入點(diǎn)。staticinitialization(TypePattern)寫入點(diǎn)的語法如下:
              pointcut <pointcout name>(<any values to be picked up>) : staticinitialization(<class>);

              staticinitialization(TypePattern)切入點(diǎn)具有兩個(gè)關(guān)鍵特征:
              1.對可供staticinitialization(TypePattern)切入點(diǎn)所選通知使用的環(huán)境有一些限制。沒有父對象觸發(fā)靜態(tài)初始化;因此,沒有this引用。也不涉及實(shí)例對象,因此,沒有目標(biāo)引用。
              2.TypePattern可以包含通配符,用于選擇一系列不同的類。

          package com.aspectj;

          public aspect StaticinitializationRecipe {
              
          /*
               * Specifies calling advice when a class is initialized
               * that meets the following type pattern rules:
               * 
               * Class Name:MyClass
               
          */

              pointcut myClassStaticInitializationPointcut() : staticinitialization(MyClass);
              
              
          //Advice declaration
              before() : myClassStaticInitializationPointcut() {
                  System.out.println(
          "---------- Aspect Advice Logic ----------");
                  System.out.println(
          "In the advice picked by myClassStaticInitializationPointcut()");
                  System.out.println(
          "Join Point Kind: ");
                  System.out.println(thisJoinPoint.getStaticPart().getKind());
                  System.out.println(
          "Signature: " + thisJoinPoint.getSignature());
                  System.out.println(
          "Source Line: " + thisJoinPoint.getSourceLocation());
                  System.out.println(
          "-----------------------------------------");        
              }

          }

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

          公告


          導(dǎo)航

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

          統(tǒng)計(jì)

          常用鏈接

          留言簿(4)

          隨筆分類

          隨筆檔案

          收藏夾

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 太仓市| 宽城| 永安市| 延长县| 平凉市| 安西县| 滁州市| 双江| 鄄城县| 宕昌县| 肃南| 锡林郭勒盟| 徐水县| 青龙| 芷江| 静宁县| 南部县| 广水市| 噶尔县| 从江县| 紫云| 高要市| 巩义市| 鄂伦春自治旗| 陵川县| 车险| 富顺县| 武川县| 永德县| 南皮县| 浮梁县| 阿图什市| 余干县| 四子王旗| 高要市| 万源市| 博乐市| 乐平市| 古丈县| 苗栗县| 中宁县|