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

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

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

    最愛Java

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

    《AspectJ Cookbook》讀書筆記十四: 定義方面實例化

    一. 定義單件(singleton)方面
            通過把issingleton()語句顯示添加到方面聲明中,來顯示建立單件方面實例化策略。

     

    package com.aspectj;

    public aspect Singleton issingleton() {
        
    /**
         * Specifies calling advice whenever a method 
         * matching the following rules gets called:
         * 
         * Class Name: MyClass
         * Method Name:foo
         * Method Return Type:void
         * Method Parameters:an int followed by a String
         
    */

        pointcut callPointCut() : call(
    void MyClass.foo(int,String));
        
        
    //Advice declartion
        before():callPointCut() && !within(Singleton+{
            System.out.println(
    "---------- Aspect Advice Logic ----------");
            System.out.println(
    "In the advice attached to the call point cut");
            System.out.println(
    "Target: " + thisJoinPoint.getTarget());
            System.out.println(
    "This: " + thisJoinPoint.getThis());
            System.out.println(
    "Aspect Instance: " + Singleton.aspectOf());
            System.out.println(
    "-----------------------------------------");        
        }

    }


            傳統(tǒng)的面向對象單件的主要缺點是:使用單件的每個類都與單件的公共接口緊密耦合。而面向方面的單件沒有這個缺點。

    二.在每個實例上定義一個方面
            AspectJ提供了perthis(Pointcut)和pertarget(Pointcut)方面實例化策略,他們依據Pointcut定義選擇的類,來聲明應該為每個新的對象實例所實例化的方面。
            perthis(Pointcut)聲明和pertarget(Poinitcut)聲明之間的區(qū)別必須涉及:在到達通知的連接點時,將會檢查什么對象。perthis(Pointcut)聲明指定:將為通知觸發(fā)連接點處的this說引用的每個新對象而實例化一個新的方面。pertarget(Pointcut)實例化策略指定:如果新對象是通知觸發(fā)連接點的目標,則為每個這樣的新對象實例化一個新的方面。

     

    package com.aspectj;

    public aspect PerThis perthis(callPointCut()) {
        
    /**
         * Specifies calling advice whenever a method matching the following rules
         * gets called:
         * 
         * Class Name: MyClass Method Name:foo Method Return Type:void Method
         * Parameters:an int followed by a String
         
    */

        pointcut callPointCut() : call(
    void MyClass.foo(int,String));

        
    // Advice declaration
        before():callPointCut()&&!within(PerThis+){
            System.out.println(
    "---------- Aspect Advice Logic ----------");
            System.out.println(
    "In the advice attached to the call point cut");
            System.out.println(
    "Target: " + thisJoinPoint.getTarget());
            System.out.println(
    "This: " + thisJoinPoint.getThis());
            System.out.println(
    "Aspect Instance: " + PerThis.aspectOf(thisJoinPoint.getThis()));
            System.out.println(
    "-----------------------------------------");
        }

    }


            在考慮可以通過單個方面通知多個類時,perthis(Pointcut)和pertarget(Pointcut)方面實例化策略聲明上的Pointcut參數提供了一些有趣的問題。以下示例中perthis(Pointcut)方面實例化策略只與executeMyClass()切入點指定的MyClass類的對象相關。

     

    package com.aspectj;

    public aspect AdviseMultipleClasses perthis(executeMyClassFoo()){
        
    public pointcut executeMyClassFoo() : execution(void MyClass.foo());
        
        
    public pointcut executeAnotherClassFoo() : execution(void AnotherClass.foo());
        
        before():executeMyClassFoo() 
    {
            System.out.println(
    "Advising foo");
            System.out.println(
    "Aspect is: " + this);
        }

        
        before():executeAnotherClassFoo() 
    {
            System.out.println(
    "Advising foo");
            System.out.println(
    "Aspect is: " + this);
        }

    }


            聲明只為executeMyClassFoo()切入點指定的每個新對象實例化AdviseMultipleClasses方面,這隱式排除了其他類的對象。即使聲明了executeAnotherClassFoo()切入點,并且他具有相應的通知,也不會導致把任何方面應用于除了它與executeMyClassFoo()共享的那些類之外的任何類。在示例中,兩個切入點之間沒有共享任何公共類,因此,executeMyClassFoo()切入點及關聯的通知會被忽略,因為這個切入點參與了perthis(Pointcut)實例化策略的定義。

            一個方面不能具有針對兩類不同對象的兩種實例化策。為了確保方面的實例化策略與它通知的類的素有對象相關,一種有用的技術是:純粹為了使用方面的實例化策略,聲明一個切入點來結合方面中的所有其他的切入點聲明,如:

     

    package com.aspectj;

    public aspect AdviseMultipleClasses perthis(executeMyClassFoo()){
        
    public pointcut executeMyClassFoo() : execution(void MyClass.foo());
        
        
    public pointcut executeAnotherClassFoo() : execution(void AnotherClass.foo());
        
        
    public pointcut applyLifecyclePolicy() : executeMyClassFoo()||executeAnotherClassFoo();
        
        before():executeMyClassFoo() 
    {
            System.out.println(
    "Advising foo");
            System.out.println(
    "Aspect is: " + this);
        }

        
        before():executeAnotherClassFoo() 
    {
            System.out.println(
    "Advising foo");
            System.out.println(
    "Aspect is: " + this);
        }

    }


    三.在每個控制流程上定義一個方面
            使用percflow(Pointcut)方面實例化聲明。percflow(Pointcut)語句指示編譯器,它應該為從Pointcut參數指定的連接點集合內進入的每個新控制流程創(chuàng)建方面的一個新實例。

    package com.aspectj;

    public aspect PerControlFlow percflow(callPointCut()){
        
    /**
         * Specifies calling advice whenever a method matching the following rules
         * gets called:
         * 
         * Class Name: MyClass Method Name:foo Method Return Type:void Method
         * Parameters:an int followed by a String
         
    */

        pointcut callPointCut() : call(
    void MyClass.foo(int,String));

        
    // Advice declaration
        before():callPointCut()&&!within(PerControlFlow+){
            System.out.println(
    "---------- Aspect Advice Logic ----------");
            System.out.println(
    "In the advice attached to the call point cut");
            System.out.println(
    "Target: " + thisJoinPoint.getTarget());
            System.out.println(
    "This: " + thisJoinPoint.getThis());
            System.out.println(
    "Aspect Instance: " + PerControlFlow.aspectOf());
            System.out.println(
    "-----------------------------------------");
        }

    }


    percflow(Pointcut)語句代表最細粒度的方面實例化策略,并且會為特定的代碼段創(chuàng)建最大蘇聯高的不同方面實例。

    posted on 2008-08-26 14:37 Brian 閱讀(460) 評論(0)  編輯  收藏 所屬分類: 《AspectJ Cookbook》讀書筆記

    公告


    導航

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

    統(tǒng)計

    常用鏈接

    留言簿(4)

    隨筆分類

    隨筆檔案

    收藏夾

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 一级毛片视频免费观看| 亚洲AV日韩综合一区| 99精品免费视频| 日韩亚洲变态另类中文| 特级毛片在线大全免费播放| gogo全球高清大胆亚洲| 国产亚洲人成在线影院| 亚洲国产精品无码久久青草| 久久久久久亚洲精品无码| 韩国免费三片在线视频| 亚洲AV无码XXX麻豆艾秋| 免费在线黄色网址| 欧洲乱码伦视频免费国产| 久久99亚洲综合精品首页 | 精品一区二区三区免费毛片爱 | 91嫩草亚洲精品| 在线v片免费观看视频| 亚洲爆乳大丰满无码专区| 免费国产美女爽到喷出水来视频| 性生大片视频免费观看一级| 国产亚洲视频在线播放| 69视频在线观看免费| 伊人久久亚洲综合影院首页| 国产免费av片在线播放| 中文字幕成人免费高清在线视频| 久久精品亚洲一区二区| 黄色片在线免费观看| 无套内谢孕妇毛片免费看看| 国产亚洲无线码一区二区 | 特级淫片国产免费高清视频| 一级大黄美女免费播放| 亚洲国产精品久久久久网站| 美女视频黄的全免费视频 | 国产亚洲蜜芽精品久久| 国产日韩亚洲大尺度高清| 国产乱码免费卡1卡二卡3卡| 精品韩国亚洲av无码不卡区| 久久亚洲高清观看| 午夜成年女人毛片免费观看| 久久WWW免费人成—看片| 亚洲毛片基地日韩毛片基地|