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

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

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

    最愛(ài)Java

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

    《AspectJ Cookbook》讀書筆記十二: 捕獲基于布爾或復(fù)合表達(dá)式的連接點(diǎn)

    一.捕獲何時(shí)連接點(diǎn)上的運(yùn)行時(shí)條件評(píng)估為true
        使用if(Expression)語(yǔ)句來(lái)評(píng)估包含要比較的運(yùn)行時(shí)變量的布爾表達(dá)式。if(Expression)語(yǔ)句的語(yǔ)法如下:
        pointcut <pointcut name>(<any values to be picked up>) : if(<Boolean expression>);

        if(Expression)語(yǔ)句有兩個(gè)關(guān)鍵特征:
            1.if(Expression)切入點(diǎn)評(píng)估在運(yùn)行時(shí)提供的變量,得到關(guān)于連接點(diǎn)是否應(yīng)該觸發(fā)相應(yīng)通知的true或false結(jié)果。
            2.Expression可以由多個(gè)邏輯元素組成,包括展示的連接點(diǎn)環(huán)境,靜態(tài)變以及其他切入點(diǎn)聲明。


    package com.aspectj;

    public aspect IfRecipe {
         
    // Define some variables for comparison
         private static final long realisticSalary=300001;

         
    /**
          * Specifies calling advice if this is referencing an object of class MyClass
          * and the object has a realistic salary:
          
    */

         pointcut ifJoinPointThishasRealisticSalaryPointcut():
    if(
                 (thisJoinPoint.getThis() 
    instanceof MyClass)
                 
    && ((MyClass) thisJoinPoint.getThis()).getSalary() < realisticSalary
                 
    && ((MyClass) thisJoinPoint.getThis()).getSalary() > 0)
             
    && !withincode(* MyClass.get*())
             
    && !execution(* MyClass.get*());
         
         
    //Advice declaration
         
    //This advice will be executed before the pointcut that picks it
            after() : ifJoinPointThishasRealisticSalaryPointcut() && !within(IfRecipe+{
                System.out.println(
    "---------- Aspect Advice Logic ----------");
                System.out.println(
    "In the advice picked by ifJoinPointThishasRealisticSalaryPointcut()");
                System.out.println(
    "Join Point Kind" + thisJoinPoint.getKind());
                System.out.println(
    "Executing object: " + thisJoinPoint.getThis());
                System.out.println(
    "MyClass instance: " + ((MyClass) thisJoinPoint.getThis()).getName() + ":" + ((MyClass) thisJoinPoint.getThis()).getSalary());
                System.out.println(
    "Signature: " + thisJoinPoint.getStaticPart().getSignature());
                System.out.println(
    "Source Line: " + thisJoinPoint.getStaticPart().getSourceLocation());
                System.out.println(
    "-----------------------------------------");        
            }
         
    }



        if(Expression)語(yǔ)句允許定義是否應(yīng)該把一份通知應(yīng)用于特定連接點(diǎn)的條件邏輯。這個(gè)條件邏輯在運(yùn)行時(shí)執(zhí)行,并且在到達(dá)連接點(diǎn)那一刻必須工作在有效的類型上。
        例子中ifJoinPointThisHasRealisticSalaryPointcut()切入點(diǎn)中包含的條件邏輯指定在發(fā)生以下事件時(shí),應(yīng)該觸發(fā)相應(yīng)的after通知:
        1.執(zhí)行對(duì)象是MyClass類型
        2.對(duì)象的salary屬性小于realisticSalary常量
        3.對(duì)象的salary屬性小于0
        4.當(dāng)前連接點(diǎn)不在getSalary()方法內(nèi)。通過(guò)使用通配符,連接點(diǎn)絕對(duì)不能出現(xiàn)在開始到達(dá)的MyClass類的任何方法中。
        
        可以使用AND(&&)運(yùn)算符來(lái)邏輯地結(jié)合每個(gè)條件。前3個(gè)條件可以相當(dāng)容易地理解成切入點(diǎn)布爾邏輯的一部分,但是最后一個(gè)條件更有趣一點(diǎn)。必須包含!withincode(* MyClass.get*())條件,以防止從通知內(nèi)調(diào)用getSalary(),這反過(guò)來(lái)又會(huì)觸發(fā)對(duì)通知的遞歸調(diào)用,并導(dǎo)致一個(gè)死循環(huán)。

    二.使用邏輯AND(&&)結(jié)合切入點(diǎn)
        使用&&運(yùn)算符。&& operator運(yùn)算符的語(yǔ)法如下:
        pointcut <pointcut name>(<any values to be picked up>) : <pointcut declaation> && <pointcut declaration>


    package com.aspectj;

    public aspect LogicalAndRecipe {
        
    /**
         * Specifies calling advice whenever a method 
         * matching the following rules gets called:
         * 
         * Class Name: MyClass
         * Method Name:Any Method
         * Method Return Type:Any Return Type
         * Method Parameters:Any Parameters
         
    */

        pointcut callAnyMethodOnMyClass():call(
    * MyClass.* (..));
        
        
    /**
         * Specifies calling advice whenever a method 
         * matching the following rules gets called:
         * 
         * Class Name: MyClass
         * Method Name:bar
         * Method Return Type:void
         * Method Parameters:None
         
    */

        pointcut callBarPointcut() : call(
    void MyClass.bar());
        
        
    /**
         * Specifies calling advice whenever a join points is 
         * encountered that would be picked by both pointcuts
         * specified:
         * 
         * Pointcut Name: callAyMethodOnMyClass
         * Pointcut Name:callBarPointcut
         * Method Return Type:void
         * Method Parameters:None
         
    */

        pointcut callIntersectionAnyAndBar():callAnyMethodOnMyClass() 
    && callBarPointcut();
        
        
    //Advice declaration
        before():callAnyMethodOnMyClass()&&!within(LogicalAndRecipe+{
            System.out.println(
    "---------- Aspect Advice Logic ----------");
            System.out.println(
    "In the advice picked by callAnyMethodOnMyClass()");
            System.out.println(
    "Signature: " + thisJoinPoint.getSignature());
            System.out.println(
    "Source Line: " + thisJoinPoint.getSourceLocation());
            System.out.println(
    "-----------------------------------------");        
        }

        
        
    //Advice declaration
        before():callBarPointcut()&&!within(LogicalAndRecipe+{
            System.out.println(
    "---------- Aspect Advice Logic ----------");
            System.out.println(
    "In the advice picked by callBarPointcut()");
            System.out.println(
    "Signature: " + thisJoinPoint.getSignature());
            System.out.println(
    "Source Line: " + thisJoinPoint.getSourceLocation());
            System.out.println(
    "-----------------------------------------");        
        }

        
        
    //Advice declaration
        before():callIntersectionAnyAndBar()&&!within(LogicalAndRecipe+{
            System.out.println(
    "---------- Aspect Advice Logic ----------");
            System.out.println(
    "In the advice picked by callIntersectionAnyAndBar()");
            System.out.println(
    "Signature: " + thisJoinPoint.getSignature());
            System.out.println(
    "Source Line: " + thisJoinPoint.getSourceLocation());
            System.out.println(
    "-----------------------------------------");        
        }
        
    }


        在AspectJ環(huán)境中,當(dāng)用&&運(yùn)算符吧兩個(gè)或多個(gè)簡(jiǎn)單的切入點(diǎn)結(jié)合進(jìn)一步復(fù)合切入點(diǎn)時(shí),被兩個(gè)獨(dú)立的切入點(diǎn)選擇的連接點(diǎn)將觸發(fā)復(fù)合切入點(diǎn)的關(guān)聯(lián)通知。更簡(jiǎn)單地說(shuō),如果任一個(gè)簡(jiǎn)單切入點(diǎn)選擇了任何一個(gè)連接點(diǎn),那么整個(gè)復(fù)合切入點(diǎn)就不會(huì)選擇這個(gè)連接點(diǎn)。
    使用&&運(yùn)算符結(jié)合切入點(diǎn)的次序還會(huì)影響復(fù)合切入點(diǎn)的解釋方式。&&運(yùn)算符的運(yùn)行時(shí)分析是從左到右執(zhí)行的。這意味著在檢查候選連接點(diǎn)時(shí),指示它不包含連接點(diǎn)的第一個(gè)切入點(diǎn)就是停止比較的地方。這對(duì)于Java中的&&運(yùn)算符是成立的,并且當(dāng)復(fù)合中的比較之一必須通過(guò)前一個(gè)條件加以保護(hù)時(shí)特別有用。

    三. 使用邏輯OR(||)結(jié)合切入點(diǎn)
        使用||運(yùn)算符。|| operator運(yùn)算符的語(yǔ)法如下:
        pointcut <pointcut name>(<any values to be picked up>) : <pointcut declaation> || <pointcut declaration>

    package com.aspectj;

    public aspect LogicalOrRecipe {
        
    /**
         * Specifies calling advice whenever a method 
         * matching the following rules gets called:
         * 
         * Class Name: MyClass
         * Method Name:foo
         * Method Return Type:void
         * Method Parameters:int and a String
         
    */

        pointcut callFooIntStringPointcut():call(
    void MyClass.foo(int,String));
        
        
    /**
         * Specifies calling advice whenever a method 
         * matching the following rules gets called:
         * 
         * Class Name: MyClass
         * Method Name:bar
         * Method Return Type:void
         * Method Parameters:None
         
    */

        pointcut callBarPointcut() : call(
    void MyClass.bar());
        
        
    /**
         * Specifies calling advice whenever a join points is 
         * encountered that would be picked by both pointcuts
         * specified:
         * 
         * Pointcut Name: callFooIntStringPointcut
         * Pointcut Name:callBarPointcut
         * Method Return Type:void
         * Method Parameters:None
         
    */

        pointcut callIntersectionFooOrBar():callFooIntStringPointcut() 
    || callBarPointcut();
        
        
    //Advice declaration
        before():callFooIntStringPointcut()&&!within(LogicalOrRecipe+{
            System.out.println(
    "---------- Aspect Advice Logic ----------");
            System.out.println(
    "In the advice picked by callFooIntStringPointcut()");
            System.out.println(
    "Signature: " + thisJoinPoint.getSignature());
            System.out.println(
    "Source Line: " + thisJoinPoint.getSourceLocation());
            System.out.println(
    "-----------------------------------------");        
        }

        
        
    //Advice declaration
        before():callBarPointcut()&&!within(LogicalOrRecipe+{
            System.out.println(
    "---------- Aspect Advice Logic ----------");
            System.out.println(
    "In the advice picked by callBarPointcut()");
            System.out.println(
    "Signature: " + thisJoinPoint.getSignature());
            System.out.println(
    "Source Line: " + thisJoinPoint.getSourceLocation());
            System.out.println(
    "-----------------------------------------");        
        }

        
        
    //Advice declaration
        before():callIntersectionFooOrBar()&&!within(LogicalOrRecipe+{
            System.out.println(
    "---------- Aspect Advice Logic ----------");
            System.out.println(
    "In the advice picked by callIntersectionFooOrBar()");
            System.out.println(
    "Signature: " + thisJoinPoint.getSignature());
            System.out.println(
    "Source Line: " + thisJoinPoint.getSourceLocation());
            System.out.println(
    "-----------------------------------------");        
        }
        
    }



        在AspectJ語(yǔ)言中,如果連接點(diǎn)在使用邏輯OR結(jié)合進(jìn)入復(fù)合切入點(diǎn)的任何成分切入點(diǎn)上啟動(dòng)通知,連接點(diǎn)就會(huì)觸發(fā)那個(gè)通知。
        AspectJ中的||運(yùn)算符還顯示了短路行為,這個(gè)&&運(yùn)算符相似且相反。

    四.捕獲所有未通過(guò)切入點(diǎn)聲明指定的連接點(diǎn)
        使用一元!運(yùn)算符,指定忽略那些通常由特定切入點(diǎn)捕獲的連接點(diǎn)。!運(yùn)算符的語(yǔ)法如下:
        pointcut <pointcut name>(<any values to be picked up>): !<pointcut declaation>


    package com.aspectj;

    public aspect LogicalNotRecipe {
        
    /**
         * Specifies calling advice whenever a method 
         * matching NOT match the following rules:
         * 
         * Class Name: MyClass
         * Method Name:foo
         * Method Return Type:void
         * Method Parameters:int and a String
         
    */

        pointcut notCallPointcutFooIntString() : 
    !call(void MyClass.foo(int,String));
        
        
    //Advice declaration
        before():notCallPointcutFooIntString()&&!within(LogicalNotRecipe+{
            System.out.println(
    "---------- Aspect Advice Logic ----------");
            System.out.println(
    "In the advice picked by callFooIntStringPointcut()");
            System.out.println(
    "Signature: " + thisJoinPoint.getSignature());
            System.out.println(
    "Source Line: " + thisJoinPoint.getSourceLocation());
            System.out.println(
    "-----------------------------------------");        
        }

    }


    五.聲明匿名切入點(diǎn)
        匿名切入點(diǎn)是切入點(diǎn)聲明的構(gòu)件。在所有基于切入點(diǎn)的章節(jié)中都會(huì)用它,但是我們?cè)谶@里更細(xì)致地關(guān)注匿名切入點(diǎn)。

    package com.aspectj;

    public aspect AnonymousPointcutRecipe {
        
    /**
         * A pointcut declaration that is built up from one
         * anonymous pointcut:
         * 
         * Anonymous Pointcuts:call(void MyClass.foo(int,String))
         
    */

        pointcut singleAnonymousPointcut() : call(
    void MyClass.foo(int,String));
        
        
    /**
         * A pointcut declaration that is built up from two
         * anonymous pointcut:
         * 
         * Anonymous Pointcuts:call(void MyClass.foo(int,String))
         *                         call(void MyClass.foo(int,String))
         *                          !within(AnonymousPointcutRecipe+)
         
    */

        pointcut multipleAnonymousPointcut() : (
                call(
    void MyClass.bar())
                
    || call(void MyClass.foo(int,String))
                
    && !within(AnonymousPointcutRecipe+));
        
        
    /**
         * A pointcut declaration attached to the advice it will invoke,
         * built up from anonymous pointcut:
         * 
         * Anonymous Pointcuts:within(LogicalOrRecipe+)
         
    */

        
    //Advice declaration
        before():singleAnonymousPointcut()&&!within(LogicalNotRecipe+{
            System.out.println(
    "---------- Aspect Advice Logic ----------");
            System.out.println(
    "In the advice picked by singleanonymousPointcut and !within(AnonymousPointcutRecipe+)");
            System.out.println(
    "Signature: " + thisJoinPoint.getSignature());
            System.out.println(
    "Source Line: " + thisJoinPoint.getSourceLocation());
            System.out.println(
    "-----------------------------------------");        
        }

        
        
    /**
         * A pointcut declaration attached to the advice it will invoke,
         * built up from anonymous pointcut:
         * 
         * Anonymous Pointcuts:None
         
    */

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


        匿名切入點(diǎn)用于聲明切入點(diǎn),而不是檢查特定的切入點(diǎn)類型。可以在命名切入點(diǎn)聲明內(nèi)匿名地聲明切入點(diǎn),或者通過(guò)把切入點(diǎn)直接附加到它們將調(diào)用的通知上來(lái)進(jìn)行聲明。

    六.重用切入點(diǎn)
        聲明一個(gè)切入點(diǎn),可以在需要重用它的地方通過(guò)名稱來(lái)引用它。

     

    package com.aspectj;

    public aspect PointcutReuseRecipe {
        
    /**
         * A pointcut declaration that is to be used and reused:
         * Anonymous Pointcuts: call(void MyClass.foo(int,String))
         
    */

        pointcut foundationNamedPointcut():call(
    void MyClass.foo(int,String));
        
        
    /**
         * A pointcut declaration that is built up from two
         * pointcuts:
         * 
         * Anonymous Pointcuts:!within(AnonymousPointcutRecipe+)
         * Named Pointcuts:foundationNamedPointcut()
         
    */

        pointcut reuseNamedPointcut():foundationNamedPointcut()
    &&!within(PointcutReuseRecipe+);
        
        
    /**
         * A pointcut declaration attached to the advice it will invoke,
         * built up from simple named and anonymous pointcuts:
         * Anonymous Pointcut:!within(LogicOrRecipe+)
         * Named Pointcuts:foundationNamedPointcut();
         
    */

        before():foundationNamedPointcut()
    &&!within(PointcutReuseRecipe+{
            System.out.println(
    "---------- Aspect Advice Logic ----------");
            System.out.println(
    "In the advice picked by foundationNamedPointcut and !within(PointcutReuseRecipe+)");
            System.out.println(
    "Signature: " + thisJoinPoint.getSignature());
            System.out.println(
    "Source Line: " + thisJoinPoint.getSourceLocation());
            System.out.println(
    "-----------------------------------------");        
        }

        
        
    /**
         * A pointcut declaration attached to the advice it will invoke,
         * built up from complex pointcut built reusing other pointcut
         * declarations:
         * 
         * Named Pointcuts:reuseNamedPointcut();
         
    */

        before():reuseNamedPointcut() 
    {
            System.out.println(
    "---------- Aspect Advice Logic ----------");
            System.out.println(
    "In the advice picked by reuseNamedPointcut()");
            System.out.println(
    "Signature: " + thisJoinPoint.getSignature());
            System.out.println(
    "Source Line: " + thisJoinPoint.getSourceLocation());
            System.out.println(
    "-----------------------------------------");        
        }
        
        
        
    }

     

    posted on 2008-08-25 20:40 Brian 閱讀(417) 評(píng)論(0)  編輯  收藏 所屬分類: 《AspectJ Cookbook》讀書筆記

    公告


    導(dǎo)航

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

    統(tǒng)計(jì)

    常用鏈接

    留言簿(4)

    隨筆分類

    隨筆檔案

    收藏夾

    搜索

    最新評(píng)論

    閱讀排行榜

    評(píng)論排行榜

    主站蜘蛛池模板: 一级看片免费视频| 美女视频黄a视频全免费网站色窝 美女被cao网站免费看在线看 | 一级人做人a爰免费视频| 91精品国产免费入口| 国产午夜免费秋霞影院| 国产亚洲综合成人91精品| 亚洲一卡2卡4卡5卡6卡在线99| 男人免费视频一区二区在线观看 | 亚洲成人黄色在线| 九九综合VA免费看| 亚洲国产精品免费观看| 久久久久亚洲爆乳少妇无| 亚洲精品国产日韩| 久久久国产精品福利免费| 国产免费怕怕免费视频观看| 色婷婷六月亚洲婷婷丁香| 免费国产污网站在线观看不要卡| 最近免费mv在线电影| av在线亚洲欧洲日产一区二区| 91亚洲自偷在线观看国产馆| 成人免费av一区二区三区| 午夜毛片不卡高清免费| 亚洲福利在线观看| 九九免费久久这里有精品23| 成年女人男人免费视频播放| 亚洲AV无码一区二区三区系列| 爱情岛论坛亚洲品质自拍视频网站| 777爽死你无码免费看一二区| 亚洲精品第一国产综合精品99| 亚洲av无码电影网| 久久这里只精品国产免费10| 免费在线精品视频| 国产亚洲精品VA片在线播放| 午夜不卡久久精品无码免费| 亚洲国产精品人人做人人爱| 亚洲熟妇自偷自拍另欧美| 18禁止看的免费污网站| 亚洲国产无套无码av电影| 免费一级毛片在线播放视频免费观看永久| 啦啦啦高清视频在线观看免费| 99久久精品国产亚洲|