最愛Java

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

          《AspectJ Cookbook》讀書筆記十五: 定義方面的關系

          一.繼承切入點定義
                  創建一個抽象類。使用合適的public、protected或default訪問修飾符在抽象方面內定義可重用的切入點邏輯。最后,把抽象方面繼承進子方面中,以重用聲明的切入點。

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

          }




          二.實現抽象切入點
                  在聲明切入點和周圍的方面時,使用abstract關鍵字,并且不要提供任何切入點邏輯。

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


          }


          三.把類繼承進方面中
              使用extends關鍵字來聲明方面擴展類。示例為一個偽日志記錄類,它代表一種現有的日志記錄機制。其目標是:重構對應用程序中日志記錄類的所有現有的調用,并把日志記錄模塊化進一個方面中,它可以更靈活地織入進應用程序中。

          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 on 2008-08-26 15:34 Brian 閱讀(286) 評論(0)  編輯  收藏 所屬分類: 《AspectJ Cookbook》讀書筆記

          公告


          導航

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

          統計

          常用鏈接

          留言簿(4)

          隨筆分類

          隨筆檔案

          收藏夾

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 郧西县| 德钦县| 利津县| 阜平县| 元阳县| 黔江区| 霸州市| 乌兰浩特市| 宿州市| 嫩江县| 抚顺县| 阜城县| 广灵县| 馆陶县| 大关县| 左权县| 温宿县| 屏边| 惠来县| 咸阳市| 古田县| 舒城县| 嘉义市| 上犹县| 汉阴县| 宁河县| 得荣县| 托克托县| 凤翔县| 长岭县| 汨罗市| 沙湾县| 乌兰浩特市| 芒康县| 观塘区| 新竹市| 马公市| 大名县| 西乌珠穆沁旗| 西平县| 顺义区|