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

          公告


          導航

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

          統計

          常用鏈接

          留言簿(4)

          隨筆分類

          隨筆檔案

          收藏夾

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 顺义区| 卢龙县| 太保市| 高台县| 绥化市| 新安县| 嘉鱼县| 西林县| 公安县| 诸城市| 永康市| 准格尔旗| 若羌县| 宕昌县| 蒙阴县| 安达市| 太白县| 广昌县| 轮台县| 怀来县| 巴中市| 青海省| 张家口市| 龙山县| 昭苏县| 阿瓦提县| 拜泉县| 松阳县| 通城县| 长葛市| 工布江达县| 翁牛特旗| 嘉兴市| 阿拉善盟| 承德县| 仁布县| 武冈市| 鸡泽县| 彝良县| 郑州市| 镇江市|