AspectJ的注解开发AOP:异常抛出通知的学习

异常抛出通知使用@AfterThrowing

在切面类中配置:

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;

import java.math.BigInteger;

@Aspect
public class aspectj {
//    @Before(value = "execution(* com.AspecJ.xiaomaoDao.*(..))")
//    public void before(){
//        System.out.println("我是前置通知!");
//    }


//    @AfterReturning(value = "execution(* com.AspecJ.xiaomaoDao.delete())",returning = "element")
//    使用returning来接受返回值
//    public void After(Object element){
//        System.out.println("我删除了"+element); //打印输出了返回值
//    }

//    @Around(value="execution(* com.AspecJ.xiaomaoDao.update())")
//    public Object around(ProceedingJoinPoint joinPoint){
//        Object obj=null;
//        System.out.println("环绕前");
//        try {
//            obj=joinPoint.proceed();
//        } catch (Throwable throwable) {
//            throwable.printStackTrace();
//        }
//        System.out.println("环绕后");
//        return obj;
//    }
    @AfterThrowing(value = "execution(* com.AspecJ.xiaomaoDao.find())",throwing = "throwinfo")
    public void throwafter(Throwable throwinfo){
        System.out.println("发生了异常,异常信息如下:");
        System.out.println(throwinfo.getMessage());
    }
}

目标类中的方法如下:

public class xiaomaoDao {
    public void save(){
        System.out.println("save xiaomao!");
    }
    public void find(){
        System.out.println("find xiaomao!");
        int i=1/0;
    }
    public String delete(){
        System.out.println("delete xiaomao!");
        return "xiaomao";
    }
    public void update(){
        System.out.println("update xiaomao!");
    }
}

执行后的结果如下:

delete xiaomao!
find xiaomao!
发生了异常,异常信息如下:
/ by zero

 

posted @ 2019-08-17 23:18  我不是小拉哈  阅读(1082)  评论(0编辑  收藏  举报