<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    最愛Java

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

    《AspectJ Cookbook》讀書筆記八: 捕獲屬性上的連接點

    一. 捕獲何時訪問對象的屬性
        使用get(Signature)切入點。get(Signature)切入點的語法如下:
        pointcut <pointcut name>(<any values to be picked up>) : get(<optional modifier> <type> <class>.<field>);
        get(Signature)具有4個關鍵特征:
        1.get(Signature)切入點會觸發直接在其中(而不僅僅是在訪問器方法的調用上)訪問屬性的通知。
        2.get(Signature)切入點不能捕獲對靜態屬性的訪問,盡管從AspectJ的語法角度講以這種方式定義切入點是完全合法的。
        3.Signature必須解析成特定類的屬性。
        4.Signature可以包含通配符,用于選擇不同屬性上的一系列連接點。

    package com.aspectj;

    public aspect GetRecipe {
        
    /**
         * Specifies calling advice whenever an attribute matching the following rules
         * is accessed:
         * 
         * Type:String 
         * Class Name:MyClass 
         * Attribute Name:name
         
    */

        pointcut getNamePointcut(): get(String MyClass.name);
        
        
    // Advice declaration
        before():getNamePointcut() {
            System.out.println(
    "---------- Aspect Advice Logic ----------");
            System.out.println(
    "In the advice picked by getNamePointcut()");
            System.out.println(
    "Signature: " + thisJoinPoint.getSignature());
            System.out.println(
    "Source Line: " + thisJoinPoint.getSourceLocation());
            System.out.println(
    "-----------------------------------------");
        }

    }

        你可能期待類使用static和final關鍵字定義一個常量屬性,這樣,在訪問這個常量時你就可能使用get(Signature)切入點來捕獲。
        

    package com.aspectj;

    public aspect GetConstant {
        
    /**
         * Specifies calling advice whenever an attribute matching the following rules
         * is accessed:
         * 
         * Type:String 
         * Class Name:MyClass 
         * Attribute Name:CONSTANT
         
    */

        pointcut getConstantPointcut():get(
    public static final String MyClass.CONSTANT);
        
        
    //Advice declaration
        before():getConstantPointcut() {
            System.out.println(
    "---------- Aspect Advice Logic ----------");
            System.out.println(
    "In the advice picked by getConstantPointcut()");
            System.out.println(
    "Signature: " + thisJoinPoint.getSignature());
            System.out.println(
    "Source Line: " + thisJoinPoint.getSourceLocation());
            System.out.println(
    "-----------------------------------------");        
        }

    }

     

    二. 捕獲訪問的字段值
        使用after returning(<ReturnValue>)形式的通知。它在聲明的returning()部分中帶有一個標識符,用于包含訪問過的值。

    package com.aspectj;

    public aspect CaptureAccessedFieldValue {
        pointcut getNamePointcut() : get(String MyClass.name);
        
        
    //Advice declaration
        after() returning(String value) : getNamePointcut() {
            System.out.println(
    "---------- Aspect Advice Logic ----------");
            System.out.println(
    "In the advice picked by getNamePointcut()");
            System.out.println(
    "Signature: " + thisJoinPoint.getStaticPart().getSignature());
            System.out.println(
    "Source Line: " + thisJoinPoint.getStaticPart().getSourceLocation());
            System.out.println(
    "-----------------------------------------");        
        }

    }


    三. 捕獲何時修改對象的字段
        使用set(Signature)切入點。set(Signature)切入點的語法如下:
        pointcut <pointcut name>(<any values to be picked up>) : set(<optional modifier> <type> <class>.<field>);
        
        set(Signature)具有4個關鍵特征:
        1.set(Signature)切入點在修改字段時觸發。
        2.set(Signature)切入點不能捕獲對靜態字段的修改,盡管從AspectJ的語法角度講以這種方式定義切入點是完全合法的。
        3.Signature必須解析成特定類的屬性。
        4.Signature可以包含通配符,用于選擇不同屬性上的一系列連接點。

    package com.aspectj;

    public aspect SetRecipe {
        
    /*
         * Specifies calling advice whenever an attribute
         * matching the following rules is modified:
         * 
         * Type: String
         * Class Name: MyClass
         * Attribute: name
         
    */

        pointcut setNamePointcut() :set(String MyClass.name);
        
        
    //Advice declaration
        before():setNamePointcut() && !within(SetRecipe+{
            System.out.println(
    "---------- Aspect Advice Logic ----------");
            System.out.println(
    "In the advice picked by setNamePointcut()");
            System.out.println(
    "Signature: " + thisJoinPoint.getSignature());
            System.out.println(
    "Source Line: " + thisJoinPoint.getSourceLocation());
            System.out.println(
    "-----------------------------------------");        
        }

    }

     

    四. 在修改字段時捕獲它的值
        結合使用args([Types | Identifiers])切入點和set(Signature)切入點,展示字段的新值,該字段被設置成切入點上的標識符,可將其傳遞給相應的通知。

    package com.aspectj;

    public aspect CaptureModifiedFieldValue {
        pointcut setNamePointcut(String newValue):set(String MyClass.name) 
    && args(newValue);
        
        
    //Advice declaration
        before(String newValue) : setNamePointcut(newValue) {
            System.out.println(
    "---------- Aspect Advice Logic ----------");
            System.out.println(
    "In the advice picked by setNamePointcut()");
            System.out.println(
    "Signature: " + thisJoinPoint.getSignature());
            System.out.println(
    "Source Line: " + thisJoinPoint.getSourceLocation());
            System.out.println(
    "-----------------------------------------");                
        }

    }

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

    公告


    導航

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

    統計

    常用鏈接

    留言簿(4)

    隨筆分類

    隨筆檔案

    收藏夾

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲成人免费在线| jzzijzzij在线观看亚洲熟妇| 无码人妻久久一区二区三区免费丨| 亚洲jjzzjjzz在线播放| 亚洲国产午夜中文字幕精品黄网站 | 亚洲精品无码永久在线观看男男| 亚洲综合区小说区激情区| 精品一区二区三区免费毛片爱| 亚洲中文字幕无码中文字| 亚洲一区二区三区自拍公司| 免费福利视频导航| 永久免费精品影视网站| 亚洲国产成人综合| 亚洲综合色在线观看亚洲| 免费看污成人午夜网站| 亚洲一区二区三区免费| 亚洲色大情网站www| 亚洲妇熟XXXX妇色黄 | 亚洲狠狠久久综合一区77777| 国产成人免费a在线资源| 嫩草在线视频www免费观看| 色偷偷尼玛图亚洲综合| 久久亚洲精品成人av无码网站| 免费成人av电影| 野花高清在线电影观看免费视频| 男人天堂免费视频| 国产亚洲精品91| 日本亚洲精品色婷婷在线影院| 亚洲精品无码av人在线观看 | 亚洲精品永久www忘忧草| 久久精品国产亚洲AV不卡| 国产精品免费观看久久| 午夜网站在线观看免费完整高清观看| 福利片免费一区二区三区| 日本亚洲免费无线码| 久久久久亚洲精品无码蜜桃| 中文亚洲AV片在线观看不卡| 免费人成在线观看播放国产 | 中字幕视频在线永久在线观看免费| 精品国产免费一区二区三区香蕉 | 亚洲另类无码专区首页|