感谢ITCAST发布的免费视频

 

例外通知会屏蔽后置通知

使用环绕通知可以代替前面所提的所有通知类型

@Aspect

public class MyInterceptor {

       @Pointcut("execution (* cn.service.impl.PersonServiceBean.*(..))")

       public void anyMethod() {} //declare a pointcut

      

       @Before("anyMethod()") //declare a before advice, parameter is the name of the pointcut

       public void doAccessCheck() {

              System.out.println("before advice");

       }

      

       @After("anyMethod()")

       public void doAfterReturning() {

              System.out.println("after advice");

       }

      

       @AfterReturning("anyMethod()")

       public void doAfter() {

              System.out.println("final advice");

       }

      

       @AfterThrowing("anyMethod()")

       public void doThrow() {

              System.out.println("throw advice");

       }    

      

       @Around("anyMethod()")

       public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {

              //if () {check authority

                     System.out.println("in");

                     Object result = pjp.proceed();

                     System.out.println("out");

              //}

             

             

              return result;

       }

}

 

如何得到输入参数,返回结果,抛出的异常?

@Aspect

public class MyInterceptor {

    @Pointcut("execution (* cn.service.impl.PersonServiceBean.*(..))")

    public void anyMethod() {} //declare a pointcut

   

    @Before("anyMethod() && args(name)") //declare a before advice, parameter is the name of the pointcut

    public void doAccessCheck(String name) {

       System.out.println("before advice");

       System.out.println(name);

    }

   

    @After("anyMethod()")

    public void doAfterReturning() {

       System.out.println("after advice");

    }

   

    @AfterReturning(pointcut = "anyMethod()", returning = "result")

    public void doAfter(String result) {

       System.out.println("final advice");

       System.out.println(result);

    }

   

    @AfterThrowing(pointcut = "anyMethod()", throwing = "ex")

    public void doThrow(Exception ex) {

       System.out.println("throw advice");

    }  

   

    @Around("anyMethod()")

    public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {

       //if () {check authority

           System.out.println("in");

           Object result = pjp.proceed();

           System.out.println("out");

       //}

      

      

       return result;

    }

}

 

posted on 2009-02-18 21:39  IT Person  阅读(403)  评论(0编辑  收藏  举报