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

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

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

    最愛Java

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

    一.繼承切入點(diǎn)定義
            創(chuàng)建一個(gè)抽象類。使用合適的public、protected或default訪問修飾符在抽象方面內(nèi)定義可重用的切入點(diǎn)邏輯。最后,把抽象方面繼承進(jìn)子方面中,以重用聲明的切入點(diǎn)。

    package com.aspectj;

    public abstract aspect BasePointcutDefinitionsAspect {
        
    public pointcut callPointcut() : call(void MyClass.foo(int,String));
    }

     

    package com.aspectj;

    public aspect ReusePointcutsRecipe extends BasePointcutDefinitionsAspect {
        
    //Advice declaration
        before():callPointcut()&&!within(ReusePointcutsRecipe+{
            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(
    "-----------------------------------------");        
        }

    }




    二.實(shí)現(xiàn)抽象切入點(diǎn)
            在聲明切入點(diǎn)和周圍的方面時(shí),使用abstract關(guān)鍵字,并且不要提供任何切入點(diǎn)邏輯。

    package com.aspectj;

    public abstract aspect BaseAbstractAspect {
        
    /**
         * Specifies an abstract pointcut placeholder
         * for derived aspects to specify
         
    */

        
    public abstract pointcut abstractBasepointcut();
        
        
    /**
         * Specifies calling advice whenever a join point
         * picked by the abstractBasePointcut (specified
         * by specialized aspects) is encountered, and not within
         * this aspect or any inheriting aspects.
         
    */

        pointcut runAdvicePointcut() : abstractBasepointcut() 
    && !within(BaseAbstractAspect+);
    }

     

    package com.aspectj;

    public aspect AbstractImplementationAspect extends BaseAbstractAspect {
        
    /**
         * Specifies calling advice whenever a method
         * matching the following rules gets called:
         * 
         * Class Name: MyClass
         * Method Name:foo
         * Method Return:void
         * Method Parameters:an int followed by a string
         
    */

        
    public pointcut abstractBasepointcut():call(void MyClass.foo(int,String));
        
        
    //Advice declaration
        before():runAdvicePointcut(){
            System.out.println(
    "---------- Aspect Advice Logic ----------");
            System.out.println(
    "Signature: " + thisJoinPoint.getStaticPart().getSignature());
            System.out.println(
    "Source Location: " + thisJoinPoint.getStaticPart().getSourceLocation());
            System.out.println(
    "-----------------------------------------");            
        }


    }


    三.把類繼承進(jìn)方面中
        使用extends關(guān)鍵字來(lái)聲明方面擴(kuò)展類。示例為一個(gè)偽日志記錄類,它代表一種現(xiàn)有的日志記錄機(jī)制。其目標(biāo)是:重構(gòu)對(duì)應(yīng)用程序中日志記錄類的所有現(xiàn)有的調(diào)用,并把日志記錄模塊化進(jìn)一個(gè)方面中,它可以更靈活地織入進(jìn)應(yīng)用程序中。

    package com.aspectj;

    public class OOLogging {
        
    public void logEntry(String entry) {
            System.out.println(
    "Entry logged: " + entry);
        }

    }

     

    package com.aspectj;

    public aspect AOLogging extends OOLogging{
        
    /**
         * Specifies calling advice whenever a method
         * matching the following rules gets called:
         * 
         * Class Name: MyClass
         * Method Name:foo
         * Method Return:void
         * Method Parameters:an int followed by a string
         
    */

        pointcut callPointcut() : call(
    void MyClass.foo(int,String));
        
        
    //Advice declaration
        before():callPointcut()&&!within(AOLogging+)&&!within(AOLogging) {
            
    this.logEntry(thisJoinPoint.toShortString());
        }

    }


     

    posted @ 2008-08-26 15:34 Brian 閱讀(286) | 評(píng)論 (0)編輯 收藏

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

     

    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)的面向?qū)ο髥渭闹饕秉c(diǎn)是:使用單件的每個(gè)類都與單件的公共接口緊密耦合。而面向方面的單件沒有這個(gè)缺點(diǎn)。

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

     

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

    }


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

     

    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()切入點(diǎn)指定的每個(gè)新對(duì)象實(shí)例化AdviseMultipleClasses方面,這隱式排除了其他類的對(duì)象。即使聲明了executeAnotherClassFoo()切入點(diǎn),并且他具有相應(yīng)的通知,也不會(huì)導(dǎo)致把任何方面應(yīng)用于除了它與executeMyClassFoo()共享的那些類之外的任何類。在示例中,兩個(gè)切入點(diǎn)之間沒有共享任何公共類,因此,executeMyClassFoo()切入點(diǎn)及關(guān)聯(lián)的通知會(huì)被忽略,因?yàn)檫@個(gè)切入點(diǎn)參與了perthis(Pointcut)實(shí)例化策略的定義。

            一個(gè)方面不能具有針對(duì)兩類不同對(duì)象的兩種實(shí)例化策。為了確保方面的實(shí)例化策略與它通知的類的素有對(duì)象相關(guān),一種有用的技術(shù)是:純粹為了使用方面的實(shí)例化策略,聲明一個(gè)切入點(diǎn)來(lái)結(jié)合方面中的所有其他的切入點(diǎn)聲明,如:

     

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

    }


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

    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)語(yǔ)句代表最細(xì)粒度的方面實(shí)例化策略,并且會(huì)為特定的代碼段創(chuàng)建最大蘇聯(lián)高的不同方面實(shí)例。

    posted @ 2008-08-26 14:37 Brian 閱讀(463) | 評(píng)論 (0)編輯 收藏
         摘要: 一.訪問類成員  package com.aspectj; public privileged aspect MemberAccessRecipe {     /** *//**      * Specifies&nb...  閱讀全文
    posted @ 2008-08-26 13:45 Brian 閱讀(1562) | 評(píng)論 (1)編輯 收藏
         摘要: 一.捕獲何時(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>...  閱讀全文
    posted @ 2008-08-25 20:40 Brian 閱讀(420) | 評(píng)論 (0)編輯 收藏
         摘要:          this([Type | Identifier])研究的是在捕捉的連接點(diǎn)處由this引用的對(duì)象的類型。連接點(diǎn)的目標(biāo)(通過target([Type | Identifier])切入點(diǎn)指定)依賴于連接點(diǎn)類型而有所不同,但它常常是在其上調(diào)用方法的對(duì)象,或者是以某種方式訪問的在其中遇到連接點(diǎn)的屬性。...  閱讀全文
    posted @ 2008-08-25 15:11 Brian 閱讀(293) | 評(píng)論 (0)編輯 收藏

        本章中描述的切入點(diǎn)支持捕獲另一個(gè)初始連接點(diǎn)作用域或環(huán)境內(nèi)的所有連接點(diǎn)。每個(gè)連接點(diǎn)在程序的控制流程中都有一個(gè)具體位置,這為通過這里描述的切入點(diǎn)聲明捕獲的連接點(diǎn)提供了環(huán)境。
        一. 捕獲通過初始連接點(diǎn)開始的程序控制流程內(nèi)的所有連接點(diǎn)
        使用cflow(Pointcut)切入點(diǎn)。cflow(Pointcut)切入點(diǎn)的語(yǔ)法如下:
        pointcut <pointcut name>(<any values to be picked up>) : cflow(<pointcut>);

        cflow(Pointcut)切入點(diǎn)具有3個(gè)關(guān)鍵特征:
            1.cflow(Pointcut)切入點(diǎn)捕獲在初始特定的連接點(diǎn)環(huán)境內(nèi)遇到的所有連接點(diǎn),這個(gè)初始連接點(diǎn)是通過另一個(gè)切入點(diǎn)選擇的。
            2.捕獲的連接點(diǎn)包括初始連接點(diǎn)。
            3.作用域是cflow(pointcut)切入點(diǎn)中重要的鑒別器。這個(gè)切入點(diǎn)將捕獲通過切入點(diǎn)參數(shù)捕獲的連接點(diǎn)的控制流程內(nèi)的所有連接點(diǎn)。


    package com.aspectj;

    public aspect CFlowRecipe {
        
    /**
         * 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 callInitialPointcut():call(
    void MyClass.foo(int,String));
        
        
    /**
         * Specifies calling advice whenever a join point is encountered
         * including and after the initial join point triggers the pointcut
         * that is specified in the parameter:
         * 
         * Pointcut Name:callInitialPointcut
         
    */

        pointcut cflowPointcut():cflow(callInitialPointcut());
        
        
    //Advice declaration
        before() : cflowPointcut() && !within(CFlowRecipe+{
            System.out.println(
    "---------- Aspect Advice Logic ----------");
            System.out.println(
    "In the advice picked by CFlowRecipe()");
            System.out.println(
    "Join Point Kind: " + thisJoinPoint.getKind());
            System.out.println(
    "Signature: " + thisJoinPoint.getStaticPart().getSignature());
            System.out.println(
    "Source Line: " + thisJoinPoint.getStaticPart().getSourceLocation());
            System.out.println(
    "-----------------------------------------");
        }
        
        

    }



            值得更詳細(xì)研究cflow(Pointcut)做什么。這個(gè)特殊的切入點(diǎn)引入了連接點(diǎn)環(huán)境的概念。它是指每個(gè)連接點(diǎn)具有一個(gè)作用域,在這個(gè)用途域內(nèi),它被看成是執(zhí)行程序的控制流程的一部分。
            在這個(gè)控制流程內(nèi),任何遇到的連接點(diǎn)都會(huì)觸發(fā)cflow(Pointcut)切入點(diǎn),并調(diào)用任何關(guān)聯(lián)的通知。當(dāng)初始連接點(diǎn)觸發(fā)指定的切入點(diǎn)參數(shù)時(shí),cflow(Pointcut)切入點(diǎn)會(huì)起作用,并觸發(fā)其關(guān)聯(lián)的通知。然后,將為在初始連接點(diǎn)環(huán)境內(nèi)的控制流程中遇到的每個(gè)連接點(diǎn)調(diào)用與cflow(Pointcut)關(guān)聯(lián)的通知。最后,捕獲的連接點(diǎn)集合包括初始連接點(diǎn)本身,這就是這個(gè)切入點(diǎn)與cflowbelow(Pointcut)切入點(diǎn)之間的主要區(qū)別。

            在cflow(Pointcut)的當(dāng)前實(shí)現(xiàn)中,在使用它時(shí),其實(shí)現(xiàn)方式會(huì)引入大量的系統(tǒng)開銷。在可能的地方,并且連接點(diǎn)重用不受影響時(shí),可以考慮優(yōu)先使用withincode(Signature)切入點(diǎn)。

        二.捕獲程序控制流程內(nèi)的所有連接點(diǎn),不包括初始連接點(diǎn)
        使用cflowbelow(Pointcut)切入點(diǎn)。cflowbelow(Pointcut)切入點(diǎn)的語(yǔ)法如下:
        pointcut <pointcut name>(<any values to be picked up>) : cflowbelow(<pointcut>);


    package com.aspectj;

    public aspect CFlowBelowRecipe {
        
    /**
         * 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 callInitialPointcut():call(
    void MyClass.foo(int,String));
        
        
    /**
         * Specifies calling advice whenever a join point is encountered
         * after the initial join point triggers the pointcut
         * that is specified in the parameter:
         * 
         * Pointcut Name:callInitialPointcut
         
    */

        pointcut cflowPointcut():cflowbelow(callInitialPointcut());
        
        
    //Advice declaration
        before() : cflowPointcut() && !within(CFlowBelowRecipe+{
            System.out.println(
    "---------- Aspect Advice Logic ----------");
            System.out.println(
    "In the advice picked by CFlowBelowRecipe()");
            System.out.println(
    "Join Point Kind: " + thisJoinPoint.getKind());
            System.out.println(
    "Signature: " + thisJoinPoint.getStaticPart().getSignature());
            System.out.println(
    "Source Line: " + thisJoinPoint.getStaticPart().getSourceLocation());
            System.out.println(
    "-----------------------------------------");
        }
        
        

    }



        這里和第一部分介紹的內(nèi)容有點(diǎn)區(qū)別;其區(qū)別是實(shí)際捕獲的連接點(diǎn)數(shù)量。cflow(Pointcut)切入點(diǎn)會(huì)觸發(fā)在初始連接點(diǎn)環(huán)境內(nèi)遇到的所有連接點(diǎn)(包括初始連接點(diǎn))上的通知,而cflowbelow(Pointcut)切入點(diǎn)則不包括那個(gè)初始連接點(diǎn)。

    posted @ 2008-08-25 10:36 Brian 閱讀(427) | 評(píng)論 (0)編輯 收藏

        切入點(diǎn)定義設(shè)計(jì)中的常用方式是:基于關(guān)注的程序作用域,限制捕獲連接點(diǎn)的范圍。這可以讓你即使控制在進(jìn)一步的切入點(diǎn)定義中將會(huì)涉及哪些連接點(diǎn)。
        本章中的切入點(diǎn)相當(dāng)容易理解,并且他們是AspectJ中一些常用的元素。例如,廣泛使用的within(TypePattern)切入點(diǎn)將以!within(%THIS_ASPECT%)形式使用它。這個(gè)AspectJ術(shù)語(yǔ)限制了當(dāng)前方面之外的每個(gè)連接點(diǎn)的作用域,從而可以防止通知觸發(fā)對(duì)相同通知塊的遞歸調(diào)用,并導(dǎo)致一個(gè)無(wú)限循環(huán)。
        一. 捕獲特定類中的所有連接點(diǎn)
        使用within(TypePattern)切入點(diǎn),利用TypePattern指定特定類類型模式,within(TypePattern)切入點(diǎn)的語(yǔ)法如下:
        pointcut <pointcut name>(<any values to be picked up>) : within(<class>);

        within(TypePattern)切入點(diǎn)具有3個(gè)關(guān)鍵特征:
            1.within(TypePattern)切入點(diǎn)捕獲指定類作用域中的所有連接點(diǎn)。
            2.within(TypePattern)切入點(diǎn)極少孤立使用。相反,它通常與切入點(diǎn)結(jié)合使用,用于減少將觸發(fā)附帶通知的連接點(diǎn)。
            3.TypePattern可以包含通配符,用于選擇不同類上的一系列連接點(diǎn)。


     

     

    package com.aspectj;

    public aspect WithinClassRecipe {
        
    /**
         * Specifies calling advice on any join point encountered within
         * the defined scope:
         * 
         * Scope:MyClass
         
    */

        pointcut withinMyClass() : within(MyClass);
        
        
    //Advice declaration
        before() : withinMyClass() && !within(WithinClassRecipe+{
            System.out.println(
    "---------- Aspect Advice Logic ----------");
            System.out.println(
    "In the advice picked by withinMyClass()");
            System.out.println(
    "Join Point Kind: " + thisJoinPoint.getKind());
            System.out.println(
    "Signature: " + thisJoinPoint.getStaticPart().getSignature());
            System.out.println(
    "Source Line: " + thisJoinPoint.getStaticPart().getSourceLocation());
            System.out.println(
    "-----------------------------------------");
        }


    }



    二. 捕獲特定包中的所有連接點(diǎn)
        within(TypePattern)切入點(diǎn)提供一種有用的手段,它使用通配符在出現(xiàn)在每個(gè)類中的連接點(diǎn)中指定一個(gè)關(guān)注??梢栽赥ypePattern中使用合適的通配符,從切入點(diǎn)邏輯的余下部分中包含或排除連接點(diǎn)的整個(gè)包。

    package com.aspectj;

    public aspect WithinPackageRecipe {
        
    /**
         * Specifies calling advice on any join point encountered within
         * the defined scope:
         * 
         * Scope:packageA
         
    */

        pointcut withinPackageA() : within(packageA.
    *);
        
        
    //Advice declaration
        before() : withinPackageA() && !within(WithinPackageRecipe+{
            System.out.println(
    "---------- Aspect Advice Logic ----------");
            System.out.println(
    "In the advice picked by WitinPackageRecipe()");
            System.out.println(
    "Join Point Kind: " + thisJoinPoint.getKind());
            System.out.println(
    "Signature: " + thisJoinPoint.getStaticPart().getSignature());
            System.out.println(
    "Source Line: " + thisJoinPoint.getStaticPart().getSourceLocation());
            System.out.println(
    "-----------------------------------------");
        }


    }


     

    三. 捕獲特定方法內(nèi)的所有連接點(diǎn)
        使用withincode(Signature)切入點(diǎn)。withincode(Signature)切入點(diǎn)的語(yǔ)法如下:
        pointcut <pointcut name>(<any values to be picked up>) : withincode(<modifier> <class>.<method>(<paramter types>));

        withincode(Signature)切入點(diǎn)有3個(gè)關(guān)鍵特征:
            1. withincode(Signature)切入點(diǎn)指定了特定方法的本地作用域內(nèi)的所有連接點(diǎn)。
            2. withincode(Signature)切入點(diǎn)極少孤立使用。相反,他通常與其他切入點(diǎn)結(jié)合使用,用于減少將觸發(fā)附帶通知的連接點(diǎn)。
            3. Signaure可以包含通配符,用于選擇不同類的不同方法上的一系列連接點(diǎn)。

    package com.aspectj;

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

        pointcut withinFooIntStringAnyReturnPointcut() : withincode(
    * MyClass.foo(int,String));
        
        
    //Advice declaration
        before() : withinFooIntStringAnyReturnPointcut() && !within(WithinMethodRecipe+{
            System.out.println(
    "---------- Aspect Advice Logic ----------");
            System.out.println(
    "In the advice picked by withinMyClass()");
            System.out.println(
    "Join Point Kind: " + thisJoinPoint.getKind());
            System.out.println(
    "Signature: " + thisJoinPoint.getStaticPart().getSignature());
            System.out.println(
    "Source Line: " + thisJoinPoint.getStaticPart().getSourceLocation());
            System.out.println(
    "-----------------------------------------");
        }


    }

    posted @ 2008-08-25 10:32 Brian 閱讀(340) | 評(píng)論 (0)編輯 收藏

    一. 捕獲何時(shí)訪問對(duì)象的屬性
        使用get(Signature)切入點(diǎn)。get(Signature)切入點(diǎn)的語(yǔ)法如下:
        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的語(yǔ)法角度講以這種方式定義切入點(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)來(lái)捕獲。
        

    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)的語(yǔ)法如下:
        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的語(yǔ)法角度講以這種方式定義切入點(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 @ 2008-08-22 10:43 Brian 閱讀(1186) | 評(píng)論 (0)編輯 收藏

    一.  捕獲對(duì)構(gòu)造函數(shù)的調(diào)用
        使用Call(Signature)寫入點(diǎn),它帶有額外的new關(guān)鍵字作為簽名的一部分。使用與構(gòu)造函數(shù)有關(guān)的call(Signature)切入點(diǎn)的語(yǔ)法如下:
        pointcut <pointcut name>(<any values to be picked up>): call(<optional modifier> <class>.new(<parameter types>));

        在用于捕獲對(duì)構(gòu)造函數(shù)的調(diào)用時(shí),call(Signature)切入點(diǎn)具有3個(gè)關(guān)鍵特征:
        1.在把一個(gè)類實(shí)例化成一個(gè)對(duì)象時(shí),具有new關(guān)鍵字的call(Signature)切入點(diǎn)會(huì)捕獲連接點(diǎn)。
        2.通過使用around()形式的通知,call(Signature)寫入點(diǎn)可以在Java的正常繼承規(guī)則的限制下重寫返回對(duì)象的類型。
        3.編譯器不會(huì)檢查指定的Signature是否對(duì)應(yīng)于實(shí)際的構(gòu)造函數(shù)。

     

    package com.aspectj;

    public aspect CallNewRecipe {
        
    /*
         * Specifies calling advice when any constructor is called
         * that meets the following signature rules:
         * 
         * Class Name:MyClass
         * Method Name:new (This is a keyword indicating the constructor call)
         * Method Parameters: int , String
         
    */

        pointcut myClassConstructorWithIntAndStringPointcut() : call(MyClass.
    new(int , String));
        
        
    //Advice declaration
        before() : myClassConstructorWithIntAndStringPointcut() {
            System.out.println(
    "---------- Aspect Advice Logic ----------");
            System.out.println(
    "In the advice picked by myClassConstructorWithIntAndOthersPointcut()");
            System.out.println(
    "The current type of object under construction is:");
            System.out.println(thisJoinPoint.getThis());
            System.out.println(
    "Signature: " + thisJoinPoint.getSignature());
            System.out.println(
    "Source Line: " + thisJoinPoint.getSourceLocation());
            System.out.println(
    "-----------------------------------------");
        }

    }



    二. 在執(zhí)行構(gòu)造函數(shù)時(shí)捕獲它
        使用execution(Signature)切入點(diǎn),它帶有額外的new關(guān)鍵字作為簽名的一部分。使用與構(gòu)造函數(shù)有關(guān)的execution(Signature)切入點(diǎn)的語(yǔ)法如下:
        pointcut <pointcut name>(<any values to be picked up>):execution(<optional modifier> <class>.new<parameter types>));

        在用于捕獲構(gòu)造函數(shù)的執(zhí)行時(shí),execution(Signature)切入點(diǎn)具有3個(gè)關(guān)鍵特征:
        1.在執(zhí)行類的構(gòu)造函數(shù)時(shí),具有new關(guān)鍵字的execution(Signature)切入點(diǎn)會(huì)觸發(fā)連接點(diǎn)。
        2.不能在調(diào)用類的構(gòu)造函數(shù)之前那一刻觸發(fā)連接點(diǎn)。這會(huì)阻止重寫返回的對(duì)象。
        3.可以使用around()通知來(lái)重寫構(gòu)造函數(shù)方法的實(shí)現(xiàn),當(dāng)不能重寫正在構(gòu)造的對(duì)象的類型。


    package com.aspectj;

    public aspect ExecutionNewRecipe {
        
    /*
         * Specifies calling advice when any constructor executes
         * that meets the following signature rules:
         * 
         * Class Name:MyClass
         * Method Name:new (This is a keyword indicating the constructor call)
         * Method Parameters: int , String
         
    */

        pointcut myClassConstructorWithIntAndStringPointcut() : execution(MyClass.
    new(int,String));
        
        
    //Advice declaration
        before() : myClassConstructorWithIntAndStringPointcut() {
            System.out.println(
    "---------- Aspect Advice Logic ----------");
            System.out.println(
    "In the advice picked by myClassConstructorWithIntAndOthersPointcut()");
            System.out.println(
    "The current type of object under construction is:");
            System.out.println(thisJoinPoint.getThis().getClass());
            System.out.println(
    "Signature: " + thisJoinPoint.getSignature());
            System.out.println(
    "Source Line: " + thisJoinPoint.getSourceLocation());
            System.out.println(
    "-----------------------------------------");        
        }

    }


    三. 捕獲何時(shí)初始化對(duì)象
        使用initialization(Signature)切入點(diǎn)。initialization(Signature)切入點(diǎn)的語(yǔ)法如下:
        pointcut <pointcut name>(<any values to be picked up>):initialization(<optional modifier> <class>.new<parameter types>));

        initialization(Signature)切入點(diǎn)具有5個(gè)關(guān)鍵特征:
        1.initialization(Signature)切入點(diǎn)必須包含new關(guān)鍵字。
        2.initialization(Signature)切入點(diǎn)捕獲連接點(diǎn)發(fā)生在任何超類的初始化之后,以及從構(gòu)造函數(shù)方法返回之前。
        3.Signature必須解析成特定類的構(gòu)造函數(shù),而不是一個(gè)簡(jiǎn)單的方法。
        4.initialization(Signature)切入點(diǎn)提供了編譯時(shí)的檢查,用于檢查構(gòu)造函數(shù)是否正在被引用。
        5.由于AspectJ編譯器中的編譯器限制,當(dāng)與around()通知關(guān)聯(lián)時(shí),不能使用initialization(Signature)切入點(diǎn)。

     

    package com.aspectj;


    public aspect InitializationRecipe {
        
    /*
         * Specifies calling advice when any object
         * initializes using a constructor
         * that meets the following signature rules:
         * 
         * Class Name:MyClass
         * Method Name:new (This is a keyword indicating the constructor call)
         * Method Parameters: int and any others
         
    */

        pointcut myClassObjectInitializationWithIntAndStringPointcut() : execution(MyClass.
    new(int,*));
        
        
    //Advice declaration
        before() : myClassObjectInitializationWithIntAndStringPointcut() {
            System.out.println(
    "---------- Aspect Advice Logic ----------");
            System.out.println(
    "In the advice picked by myClassObjectInitializationWithIntAndStringPointcut()");
            System.out.println(
    "The current type of object under construction is:");
            System.out.println(thisJoinPoint.getThis().getClass());
            System.out.println(
    "Signature: " + thisJoinPoint.getSignature());
            System.out.println(
    "Source Line: " + thisJoinPoint.getSourceLocation());
            System.out.println(
    "-----------------------------------------");        
        }

    }



    四. 捕獲何時(shí)將要初始化一個(gè)對(duì)象
        使用preinitialization(Signature)切入點(diǎn)。preinitialization(Signature)切入點(diǎn)的語(yǔ)法如下:
        pointcut <pointcut name>(<any values to be picked up>):preinitialization(<optional modifier> <class>.new<parameter types>));

        preinitialization(Signature)切入點(diǎn)具有5個(gè)關(guān)鍵特征:
        1.preinitialization(Signature)切入點(diǎn)必須包含new關(guān)鍵字。
        2.preinitialization(Signature)切入點(diǎn)捕獲連接點(diǎn)發(fā)生在進(jìn)入捕獲構(gòu)造函數(shù)之后,以及調(diào)用任何超類構(gòu)造函數(shù)之前。
        3.Signature必須解析成一個(gè)構(gòu)造函數(shù)。
        4.preinitialization(Signature)切入點(diǎn)提供了編譯時(shí)的檢查,用于檢查構(gòu)造函數(shù)是否正在被引用。
        5.由于AspectJ編譯器中的編譯器限制,當(dāng)與around()通知關(guān)聯(lián)時(shí),不能使用preinitialization(Signature)切入點(diǎn)。

     

    package com.aspectj;

    public aspect PreInitializationRecipe {
        
    /*
         * Specifies calling advice just before an object initializes
         * using a constructor that meets the following signature rules:
         * 
         * Class Name:MyClass
         * Method Name:new (This is a keyword indicating the constructor call)
         * Method Parameters: an int followed by a String
         
    */

        pointcut myClassIntStringObjectPreInitializationPointcut() : preinitialization(MyClass.
    new(int,String));
        
        
    //Advice declaration
        before(int number , String name) : myClassIntStringObjectPreInitializationPointcut() && args(number , name) {
            System.out.println(
    "---------- Aspect Advice Logic ----------");
            System.out.println(
    "In the advice picked by anyMyClassObjectInitializationPointcut()");
            System.out.println(
    "The current type of object under construction is:");
            System.out.println(thisJoinPoint.getThis());
            System.out.println(
    "The values passed in where: " + number + " , " + name);
            System.out.println(
    "Signature: " + thisJoinPoint.getSignature());
            System.out.println(
    "Source Line: " + thisJoinPoint.getSourceLocation());
            System.out.println(
    "-----------------------------------------");        
        }

    }



    五. 捕獲何時(shí)初始化類
        使用staticinitialization(TypePattern)寫入點(diǎn)。staticinitialization(TypePattern)寫入點(diǎn)的語(yǔ)法如下:
        pointcut <pointcout name>(<any values to be picked up>) : staticinitialization(<class>);

        staticinitialization(TypePattern)切入點(diǎn)具有兩個(gè)關(guān)鍵特征:
        1.對(duì)可供staticinitialization(TypePattern)切入點(diǎn)所選通知使用的環(huán)境有一些限制。沒有父對(duì)象觸發(fā)靜態(tài)初始化;因此,沒有this引用。也不涉及實(shí)例對(duì)象,因此,沒有目標(biāo)引用。
        2.TypePattern可以包含通配符,用于選擇一系列不同的類。

    package com.aspectj;

    public aspect StaticinitializationRecipe {
        
    /*
         * Specifies calling advice when a class is initialized
         * that meets the following type pattern rules:
         * 
         * Class Name:MyClass
         
    */

        pointcut myClassStaticInitializationPointcut() : staticinitialization(MyClass);
        
        
    //Advice declaration
        before() : myClassStaticInitializationPointcut() {
            System.out.println(
    "---------- Aspect Advice Logic ----------");
            System.out.println(
    "In the advice picked by myClassStaticInitializationPointcut()");
            System.out.println(
    "Join Point Kind: ");
            System.out.println(thisJoinPoint.getStaticPart().getKind());
            System.out.println(
    "Signature: " + thisJoinPoint.getSignature());
            System.out.println(
    "Source Line: " + thisJoinPoint.getSourceLocation());
            System.out.println(
    "-----------------------------------------");        
        }

    }

    posted @ 2008-08-22 10:32 Brian 閱讀(1239) | 評(píng)論 (0)編輯 收藏

        在Java中拋出異常時(shí),會(huì)將其向上傳遞給調(diào)用者,直到它被作為try/catch塊一部分的catch語(yǔ)句處理或者到達(dá)Java運(yùn)行庫(kù)并在控制臺(tái)引發(fā)一條混亂的消息。如果捕獲到異常,就應(yīng)該將異常作為一個(gè)對(duì)象傳遞給catch塊做合適的處理。同時(shí),還有一種可能需要方面作為橫切行為做一部分事情,或者替代catch塊的正常行為。
    一. 捕獲何時(shí)捕捉異常
        使用handler(TypePattern)切入點(diǎn)。其語(yǔ)法如下:
        pointcut <pointcut name>(<any values to be picked up>) : handler(<class>):
     
       handler(TypePattern)切入點(diǎn)具有5個(gè)關(guān)鍵特征:
       1. handler(TypePattern)在捕獲異常的作用域內(nèi)選擇連接點(diǎn)。
       2. handler(TypePattern)切入點(diǎn)的通知僅用于類型模式指定Throwable或其子類的地方。
       3. TypePattern聲明無(wú)論何時(shí)捕捉到異常或其子類的匹配模式,都會(huì)應(yīng)用相應(yīng)的通知。
       4. handler(TypePattern)切入點(diǎn)只支持before()形式的通知。這意味著不能使用像around()這樣的通知來(lái)重寫catch塊的正常行為。
       5. TypePattern可以包含通配符,用于選擇不同類上的一系列連接點(diǎn)。
    帶有通配符的TypePattern 描述
    mypackage..* 捕獲mypackage包及其子包中的連接點(diǎn)類
    MyClass+ 捕獲MyClass類及其任何子類中的連接點(diǎn)

        下面的例子展示了捕獲任何類拋出MyException類異常:

    package com.aspectj;

    public aspect HandlerRecipe {
        
    /**
         * Specifies calling advice when any exception object
         * is caught that matches the following rules for its 
         * type pattern;
         * 
         * Type:MyException
         
    */

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

    }


    二. 捕獲拋出的異常
        結(jié)合使用args([Types | Identifiers])切入點(diǎn) 與handler(TypePattern)切入點(diǎn),將捕獲的異常展示為切入點(diǎn)上的標(biāo)識(shí)符,可將其傳遞給相應(yīng)的通知。
    package com.aspectj;

    public aspect AccessThrownException {
        pointcut myExceptionHandlerPointout(MyException exception) : handler(MyException) 
    && args(exception);
        
        
    //Advice declaration
        before(MyException exception) : myExceptionHandlerPointout(exception) {
            System.out.println(
    "------------------- Aspect Advice Logic -------------------");
            System.out.println(
    "In the advice picked by " + "myExceptionHandlerPointcut()");
            System.out.println(
    "Signature: " + thisJoinPoint.getStaticPart().getSignature());
            System.out.println(
    "Source Line: " + thisJoinPoint.getStaticPart().getSourceLocation());
            System.out.println(
    "Exception caught:");
            exception.printStackTrace();
            System.out.println(
    "------------------------------------------");
        }

    }
       
    posted @ 2008-07-11 09:28 Brian 閱讀(1617) | 評(píng)論 (1)編輯 收藏
    僅列出標(biāo)題
    共5頁(yè): 上一頁(yè) 1 2 3 4 5 下一頁(yè) 

    公告


    導(dǎo)航

    <2025年7月>
    293012345
    6789101112
    13141516171819
    20212223242526
    272829303112
    3456789

    統(tǒng)計(jì)

    常用鏈接

    留言簿(4)

    隨筆分類

    隨筆檔案

    收藏夾

    搜索

    最新評(píng)論

    閱讀排行榜

    評(píng)論排行榜

    主站蜘蛛池模板: 亚洲精品tv久久久久久久久| 中国一级毛片视频免费看| 在线视频免费国产成人| 亚洲色大成网站www永久网站| 久草视频在线免费| 91嫩草亚洲精品| 91香蕉成人免费网站| 亚洲图片激情小说| 18禁免费无码无遮挡不卡网站| 亚洲激情黄色小说| 免费三级毛片电影片| 67194在线午夜亚洲| 大地资源在线观看免费高清| 亚洲人成网站色在线观看| 最近2019中文免费字幕| 亚洲码欧美码一区二区三区| 日本不卡视频免费| 美女被免费网站视频在线| 亚洲成人影院在线观看| 国产成人无码免费网站| 国产亚洲一区二区精品| 日韩精品人妻系列无码专区免费| 亚洲精品视频免费在线观看| AV大片在线无码永久免费| 亚洲欧美自偷自拍另类视| 国产一区视频在线免费观看| 成人久久久观看免费毛片| 亚洲真人无码永久在线| 中文字幕精品亚洲无线码二区 | 精品亚洲成a人片在线观看少妇| 在线日本高清免费不卡| 亚洲AV无码一区二区三区人| 免费激情视频网站| 杨幂最新免费特级毛片| 亚洲色婷婷一区二区三区| 亚洲a一级免费视频| 亚洲人成日本在线观看| 免费高清小黄站在线观看| 免费国产高清毛不卡片基地 | 在线jlzzjlzz免费播放| 黄色片网站在线免费观看|