posts - 431,  comments - 344,  trackbacks - 0

          使用handler(TypePattern)切入點。語法:
          pointcut <pointcut name>(<any values to be picked uo>) : handler(<class>) ;
          handler(TypePattern)切入點只支持vbefore()形式的通知。這意味著不能使用像around()這樣的通知來重寫catch塊的正常行為。

          public aspect HandlerRecipe {
           pointcut myExceptionHandlerPointcut() : handler(ServiceException);
           before() : myExceptionHandlerPointcut() {
            System.out.println("------------ Aspect Advice Logic --------------");
            System.out.println("Signature: " + thisJoinPoint.getStaticPart().getSignature());
            System.out.println("Source Line: " + thisJoinPoint.getStaticPart().getSourceLocation());
            System.out.println("-----------------------------------------------");
           }
          }


          public class ServiceException extends Exception {

           /**
            * Create a new ServiceException with the specified message.
            * @param msg the detail message
            */
           public ServiceException(String msg) {
            super(msg);
           }

           /**
            * Create a new ServiceException with the specified message
            * and root cause.
            * @param msg the detail message
            * @param ex the root cause
            */
           public ServiceException(String msg, Throwable ex) {
            super(msg, ex);
           }
          }

          public class ExceptionClass {

           public void triggerMyException() throws ServiceException
              {
            ServiceException myException = new ServiceException("A service exception has occured");
                 System.out.println("About to throw a ServiceException");
                 throw myException;
              }
           /**
            * @param args
            */
           public static void main(String[] args) {
            ExceptionClass myObject = new ExceptionClass();
             try
                 {
                    myObject.triggerMyException();
                 } catch (ServiceException me)
                 {
                    System.out.println("A ServiceException has been caught");
                 }
           }

          }

          運行結果:
          About to throw a ServiceException
          ------------ Aspect Advice Logic --------------
          Signature: catch(ServiceException)
          Source Line: ExceptionClass.java:19
          -----------------------------------------------
          A ServiceException has been caught

          獲取拋出的異常
          public aspect HandlerRecipe {
           pointcut myExceptionHandlerPointcut(ServiceException exception) : handler(ServiceException) && args(exception);
           before(ServiceException exception) : myExceptionHandlerPointcut(exception) {
            System.out.println("------------ Aspect Advice Logic --------------");
            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 on 2007-07-03 16:32 周銳 閱讀(312) 評論(0)  編輯  收藏 所屬分類: AspectJ
          主站蜘蛛池模板: 龙井市| 通许县| 呼玛县| 墨竹工卡县| 东至县| 鹤庆县| 新闻| 德化县| 麻阳| 南安市| 石嘴山市| 蒙城县| 灵璧县| 逊克县| 南靖县| 佛冈县| 教育| 中阳县| 崇义县| 岑溪市| 雅安市| 炎陵县| 安陆市| 陇西县| 白朗县| 茌平县| 福州市| 辽源市| 千阳县| 博爱县| 嫩江县| 游戏| 利川市| 奎屯市| 桃源县| 长春市| 靖西县| 丰宁| 南京市| 肥东县| 和硕县|