感谢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;

    }

}

 

相关文章:

  • 2021-09-10
  • 2021-07-12
  • 2021-08-17
  • 2021-11-21
  • 2022-01-02
  • 2022-12-23
  • 2022-12-23
  • 2021-06-10
猜你喜欢
  • 2021-09-10
  • 2021-11-20
  • 2021-10-16
  • 2022-01-29
  • 2021-11-13
  • 2022-12-23
  • 2021-08-27
相关资源
相似解决方案