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

          隨筆分類

          隨筆檔案

          收藏夾

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 大关县| 平定县| 信阳市| 阿拉善盟| 博白县| 长兴县| 界首市| 潼关县| 玉屏| 河曲县| 遂川县| 霍山县| 皋兰县| 涟源市| 梓潼县| 石首市| 陵川县| 德庆县| 无为县| 年辖:市辖区| 鹤山市| 定兴县| 定西市| 四川省| 阳泉市| 荔浦县| 台湾省| 东平县| 沅江市| 福海县| 安国市| 德惠市| 漳州市| 陇西县| 铜梁县| 元阳县| 秦安县| 罗甸县| 云龙县| 青州市| 巴彦淖尔市|