posts - 431,  comments - 344,  trackbacks - 0

          1.   捕獲對構造函數的調用
                 pointcut <pointcut name>(<any values to be picked up>) : call(<optional modifier> <class>.new(<parameter types>));

          2.   在執行構造函數時捕獲它
                pointcut <pointcut name>(<any values to be picked up>) : execution(<optional modifier> <class>.new(<parameter types>));

          3.   捕獲何時初始化對象
                pointcut <pointcut name>(<any values to be picked up>) : initialization(<optional modifier> <class>.new(<parameter types>));
                initialization(Signature)切入點必須包含new關鍵字,Signature必須解析成特定類的構造函數,而不是一個簡單的方法。
                由于AspectJ編譯器中的編譯器限制,當與around()通知關聯時,不能使用initialization(Signature)切入點。
                與execution(Signature)切入點相比,使用initialization(Signature)切入點最大優點是:它提供了編譯時檢查,以確保簽名實際上制定了一個構造函數。

          4.   捕獲何時將要初始化一個對象
                pointcut <pointcut name>(<any values to be picked up>) : preinitialization(<optional modifier> <class>.new(<parameter types>));

          5.   捕獲何時初始化類
                
          pointcut <pointcut name>(<any values to be picked up>) : staticinitialization(<class>);


          package com.eric.aspectj;

          public aspect CallNewRecipe {
           pointcut myClassConstructorWithIntAndStringPointcutStaticinitialization() : staticinitialization(MyClass);
           pointcut myClassConstructorWithIntAndStringPointcutCall() : call(MyClass.new(int, String));
           pointcut myClassConstructorWithIntAndStringPointcutExecution() : execution(MyClass.new(int, String));
           pointcut myClassConstructorWithIntAndStringPointcutPreinitialization() : preinitialization(MyClass.new(int, String));
           pointcut myClassConstructorWithIntAndStringPointcutInitialization() : initialization(MyClass.new(int, String));
           
           
           before() : myClassConstructorWithIntAndStringPointcutExecution() {
            System.out.println("-------------- Aspect Advice Logic Execution ---------------");
            System.out.println("In the advice picked by " + "myClassConstructorWithIntAndOthersPointcut()");
            System.out.println("The current type of object under construction is: ");
            System.out.println("getThis: " + thisJoinPoint.getThis());
            System.out.println("getTarget: " + thisJoinPoint.getTarget());
            System.out.println("getKind: " + thisJoinPoint.getKind());
            System.out.println("Signature: " + thisJoinPoint.getSignature());
            System.out.println("getName: " + thisJoinPoint.getSignature().getName());
            System.out.println("getDeclaringTypeName: " + thisJoinPoint.getSignature().getDeclaringTypeName());
            System.out.println("getDeclaringType: " + thisJoinPoint.getSignature().getDeclaringType());
            System.out.println("getModifiers: " + thisJoinPoint.getSignature().getModifiers());
            System.out.println("Source Line: " + thisJoinPoint.getSourceLocation());
            System.out.println("toString: " + thisJoinPoint.toString());
            System.out.println("--------------------------------------------------");
           }
           before() : myClassConstructorWithIntAndStringPointcutStaticinitialization() {
            System.out.println("--------------- Staticinitialization ------------------");
           }
           before() : myClassConstructorWithIntAndStringPointcutCall() {
            System.out.println("--------------- Call ------------------");
           }
           before() : myClassConstructorWithIntAndStringPointcutPreinitialization() {
            System.out.println("--------------- Preinitialization ------------------");
           }
           before() : myClassConstructorWithIntAndStringPointcutInitialization() {
            System.out.println("--------------- Initialization ------------------");
           }
          }

          package com.eric.aspectj;

          public class MyClass {

           private int number;
           private String name;
           
           public MyClass(int number, String name) {
            this.number = number;
            this.name = name;
           }
           /**
            * @param args
            */
           public static void main(String[] args) {
            // TODO Auto-generated method stub
            MyClass myObject = new MyClass(123, "Eric Chau");
           }

          }

          運行結果:
          --------------- Staticinitialization ------------------
          --------------- Call ------------------
          --------------- Preinitialization ------------------
          --------------- Initialization ------------------
          -------------- Aspect Advice Logic Execution ---------------
          In the advice picked by myClassConstructorWithIntAndOthersPointcut()
          The current type of object under construction is:
          getThis: com.eric.aspectj.MyClass@10d448
          getTarget: com.eric.aspectj.MyClass@10d448
          getKind: constructor-execution
          Signature: com.eric.aspectj.MyClass(int, String)
          getName: <init>
          getDeclaringTypeName: com.eric.aspectj.MyClass
          getDeclaringType: class com.eric.aspectj.MyClass
          getModifiers: 1
          Source Line: MyClass.java:8
          toString: execution(com.eric.aspectj.MyClass(int, String))
          --------------------------------------------------

          posted on 2007-07-04 13:56 周銳 閱讀(260) 評論(0)  編輯  收藏 所屬分類: AspectJ
          主站蜘蛛池模板: 巧家县| 拜城县| 新民市| 望江县| 广丰县| 交口县| 高州市| 西藏| 扶绥县| 岳普湖县| 吉安县| 武隆县| 防城港市| 清涧县| 北安市| 勃利县| 河北省| 江川县| 尖扎县| 瑞金市| 北安市| 玛沁县| 长垣县| 尉犁县| 鱼台县| 环江| 宜丰县| 仲巴县| 河池市| 长宁区| 若尔盖县| 辽阳县| 郁南县| 福海县| 洪洞县| 高密市| 泌阳县| 崇州市| 岚皋县| 旬邑县| 林芝县|