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

原文地址:https://www.cnblogs.com/xiaolaha/p/11370888.html

时间: 2024-08-29 17:42:28

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

AspectJ的注解开发AOP:最终通知的学习

最终通知的注解为@After 无论是否发生异常,最终通知总是被执行 在切面类中如下定义使用: @After(value = "execution(* com.AspecJ.xiaomaoDao.find())") public void after(){ System.out.println("最终通知执行了!"); } 原文地址:https://www.cnblogs.com/xiaolaha/p/11370918.html

AspectJ的注解开发AOP:环绕通知的学习

环绕通知的注解为@Aroundaround方法的返回值就是目标代理方法执行返回值参数ProceedingJoinPoint可以拦截目标方法的执行切面类配置如下i mport org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotat

AspectJ的注解开发AOP:切点定义

通过@Pointcut为切点命名,方便我们统一管理 在每个通知内定义切点,会造成工作量大,不易维护,对于重复的切点,可以使用@Pointcut进行定义 切点方法:private void无参方法,方法名为切点名 当通知多个切点时,可以使用||来进行连接 具体代码: import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import java.math.BigInteger; @Aspe

异常抛出与捕获的思考

异常处理的思考 在java中异常分为两类.一.检查性异常.二.非检查性异常(运行时异常) 二者的区别:检查性异常需要显式try-catch或者throw.运行时异常可以不用捕获. 对于检查性异常由于必须捕获,所有并不需要太多的讨论(在设计异常的时候需要考虑).主要讨论运行时异常的抛出与捕获. 运行时异常的正确捕获和抛出,能够让系统代码更加简洁与rd只需要关系正常业务,对于不正常业务则交由异常处理. 现在存在的困扰: 1.每调用一个其他方法,都需要考虑与分析这个方法是不是存在异常,影响正常业务流程

Python之异常抛出机制

异常抛出机制 : 常见的Python异常:

PLSQL_Oracle Exception异常分类、异常抛出、异常处理、异常传播(概念)

2014-06-03 BaoXinjian 一.摘要 在PLSQL程序开发过程中,很重要的部分就是对程序异常的监控和处理,包括如何触发异常,何时进行处理,如何进行处理,是否将程式中的所有异常集中在一起,通过公共异常处理的procedure或function,如果没有完善的程式处理机制,很难说该程式是一只健壮的程式,当程式遇到很多类型或者量很多资料时,系统若没有异常处理必然会导致程式的出错 当预判到了某些异常,需要对预判到的异常进行合适相应的处理,是否抛出异常还是忽略还是其他 当然程式没有预判到或

swif-throws异常抛出

import UIKit enum VendingMachineError: Error { case invalidSelection //选择无效 case insufficientFunds(coinsNeeded: Int) //金额不足 case outOfStock //缺货 } struct Item { var price: Int var count: Int } class VendingMachine { var inventory = [ "Candy Bar"

C++中异常规格(异常抛出表)和 成员初始化表 的放置先后顺序

测试代码如下 #include <iostream> #include <cstdio> class CPoint{ public: CPoint(int x){ printf("has synax\n"); } CPoint(){ printf("no synax\n"); } }; class Point : public CPoint{ public: Point()throw() :CPoint(1){ } }; Point p; i

(转)spring异常抛出触发事务回滚策略

背景:在面试时候问到事务方法在调用过程中出现异常,是否会传递的问题,平时接触的比较少,有些懵逼. spring异常抛出触发事务回滚策略 Spring.EJB的声明式事务默认情况下都是在抛出unchecked exception后才会触发事务的回滚 测试用业务逻辑方法: /** * 如果在spring事务配置中不为切入点(如这里的切入点可以定义成test*)配置事务在什么情况下回滚(格式:-引起回滚的异常类型) * 则spring默认只会在service方法抛出unchecked exceptio