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
          主站蜘蛛池模板: 永年县| 山阳县| 营口市| 保山市| 马山县| 阿尔山市| 墨竹工卡县| 扶绥县| 天气| 建阳市| 和顺县| 黄浦区| 五大连池市| 清徐县| 农安县| 汉川市| 蕲春县| 大丰市| 苏尼特左旗| 黄冈市| 神池县| 佛冈县| 平潭县| 历史| 东兰县| 虎林市| 通化市| 谷城县| 安徽省| 阿勒泰市| 新邵县| 抚宁县| 新干县| 南雄市| 陇南市| 邢台县| 林甸县| 文水县| 汤阴县| 万宁市| 富源县|