关于spring的ThrowsAdvice (转)


关于SPRING的AOP

aop的一个切面接口是 ThrowsAdvice,这是个标记接口,里面没有定义任何方法。书上说,根据spring文档,必须定义一个 afterThrowing([Method, args, target], subclassOfThrowable) 形式的方法,前面三个参数可选,也就是你可以写成 afterThrowing( args, target, subclassOfThrowable) ,也可以写成 afterThrowing( target, subclassOfThrowable) 

事实上如果真的这么做,运行时会抛出 At least one handler method must be found in class 形式的异常。在确认自己没有打错字之后,只好去查spring2.0的手册,才发现上面是这么说的:方法可以有一个或四个参数。 也就是说,不能有两个、三个参数,方法的形式只能有两种: afterThrowing([Method, args, target], subclassOfThrowable)  或者 afterThrowing( subclassOfThrowable)

如下例:

import java.lang.reflect.Method;

import org.springframework.aop.ThrowsAdvice;
import org.springframework.aop.framework.ProxyFactory;

public class ExceptionAdvisor implements ThrowsAdvice {
    public void afterThrowing(RuntimeException rx) {

    }

    /**
     * 对未知异常的处理.
     */
    public void afterThrowing(Method method, Object[] args, Object target, Exception ex) throws Throwable {
        System.out.println("*************************************");
        System.out.println("Error happened in class: " + target.getClass().getName());
        System.out.println("Error happened in method: " + method.getName());

        for (int i = 0; i < args.length; i++) {
            System.out.println("arg[" + i + "]: " + args[i]);
        }

        System.out.println("Exception class: " + ex.getClass().getName());
        System.out.println("*************************************");
    }

    /**
     * 对NumberFormatException异常的处理
     */
    public void afterThrowing(Method method, Object[] args, Object target, NumberFormatException ex) throws Throwable {
        System.out.println("*************************************");
        System.out.println("Error happened in class: " + target.getClass().getName());
        System.out.println("Error happened in method: " + method.getName());

        for (int i = 0; i < args.length; i++) {
            System.out.println("args[" + i + "]: " + args[i]);
        }

        System.out.println("Exception class: " + ex.getClass().getName());
        System.out.println("*************************************");
    }

    public static void main(String[] args) {
        TestBean bean = new TestBean();
        ProxyFactory pf = new ProxyFactory();
        pf.setTarget(bean);
        pf.addAdvice(new ExceptionAdvisor());

        TestBean proxy = (TestBean) pf.getProxy();
        try {
            proxy.method1();
        } catch (Exception ignore) {
            System.out.println("Exception in method1 catch");
        }

        try {
            proxy.changeToNumber("amigo");
        } catch (Exception ignore) {
            System.out.println("Exception in changeToNumber catch");
        }
    }
}

转载于:https://my.oschina.net/cronie/blog/95797

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值