最愛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)

          隨筆分類

          隨筆檔案

          收藏夾

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 秭归县| 札达县| 台东市| 方山县| 定安县| 沈阳市| 宜昌市| 伊金霍洛旗| 游戏| 饶平县| 中西区| 民和| 铜山县| 涿州市| 大余县| 桐城市| 鹤壁市| 桑植县| 紫金县| 建阳市| 东城区| 仁怀市| 垦利县| 碌曲县| 四会市| 墨江| 监利县| SHOW| 舒城县| 察雅县| 洪泽县| 蕉岭县| 称多县| 建湖县| 玉山县| 巨野县| 广州市| 大方县| 治多县| 同德县| 日喀则市|