Spring 基于Aspectj的注解开发AOP(四)-异常抛出通知@AfterThrowing

附:参考Spring 基于Aspectj的注解开发AOP(一)-前置通知

目标方法:

 public void findOne() {
        System.out.println("查询一个商品");
    }

切面 : 异常抛出通知 

 @AfterThrowing(value = "execution(* com.lucifer.aspectj.demo.ProductDao.findOne(..))")
    public void afterThrowing(){
        System.out.println("异常抛出通知=======================");
    }

控制台打印:

Spring 基于Aspectj的注解开发AOP(四)-异常抛出通知@AfterThrowing

ps:此时可以看到控制台并没有打印异常抛出通知,为什么呢???

因为目标方法中并没有抛出异常;

对以上代码进行改造,伪造异常;

 public void findOne() {
        System.out.println("查询一个商品");
        int i=1/0;
    }
    @AfterThrowing(value = "execution(* com.lucifer.aspectj.demo.ProductDao.findOne(..))",throwing = "throwable")
    public void afterThrowing(Throwable throwable){
        System.out.println("异常抛出通知======================="+throwable);
    }

ps: throwing属性和Throwable参数,可以打印出异常信息;如图:

Spring 基于Aspectj的注解开发AOP(四)-异常抛出通知@AfterThrowing