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

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

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

    最愛Java

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

    《AspectJ Cookbook》讀書筆記八: 捕獲屬性上的連接點(diǎn)

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

    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關(guān)鍵字定義一個(gè)常量屬性,這樣,在訪問這個(gè)常量時(shí)你就可能使用get(Signature)切入點(diǎn)來捕獲。
        

    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()部分中帶有一個(gè)標(biāo)識(shí)符,用于包含訪問過的值。

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

    }


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

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

    }

     

    四. 在修改字段時(shí)捕獲它的值
        結(jié)合使用args([Types | Identifiers])切入點(diǎn)和set(Signature)切入點(diǎn),展示字段的新值,該字段被設(shè)置成切入點(diǎn)上的標(biāo)識(shí)符,可將其傳遞給相應(yīng)的通知。

    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) 評(píng)論(0)  編輯  收藏 所屬分類: 《AspectJ Cookbook》讀書筆記

    公告


    導(dǎo)航

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

    統(tǒng)計(jì)

    常用鏈接

    留言簿(4)

    隨筆分類

    隨筆檔案

    收藏夾

    搜索

    最新評(píng)論

    閱讀排行榜

    評(píng)論排行榜

    主站蜘蛛池模板: 一本色道久久88综合亚洲精品高清| 成在人线av无码免费高潮喷水| 亚洲精品免费网站| 亚洲AV无码不卡无码| 无码日韩精品一区二区免费暖暖| 国产专区一va亚洲v天堂| 无码日韩人妻AV一区免费l| www.亚洲精品.com| 一级做a爰性色毛片免费| 亚洲中文字幕无码日韩| 成人免费区一区二区三区| 亚洲国产精品线在线观看| 18女人水真多免费高清毛片| 亚洲免费视频观看| 日本高清免费中文字幕不卡| 九九精品国产亚洲AV日韩| 亚洲AV无码乱码在线观看| 最新久久免费视频| 亚洲日韩中文字幕天堂不卡| 女人与禽交视频免费看| 五月天婷婷免费视频| 亚洲AV永久青草无码精品| 亚洲免费一级视频| 国产精品无码亚洲精品2021 | 特级毛片在线大全免费播放| 国产亚洲欧洲Aⅴ综合一区| 99热在线观看免费| 亚洲综合欧美色五月俺也去| 亚洲国产成人久久综合野外| 久久青草免费91线频观看不卡| 亚洲免费一级视频| 亚洲高清免费视频| 99免费观看视频| 亚洲AV网一区二区三区| 亚洲国产精彩中文乱码AV| 成年人网站在线免费观看| eeuss影院免费直达入口| 亚洲第一精品电影网| 亚洲国产成人乱码精品女人久久久不卡 | 亚洲国产另类久久久精品小说| 波多野结衣在线免费观看|