转:http://www.blogjava.net/amigoxie/archive/2007/07/24/132142.html(此原文有一点小错误O(∩_∩)O~)
Spring支持四种拦截类型:目标方法调用前(before),目标方法调用后(after),目标方法调用前后(around),以及目标方法抛出异常(throw)。
最近用到spring的AOP来实现异常拦截,用到了spring的ThrowsAdvice。ThrowsAdvice是一个标示接口,我们可以在类中定义一个或多个,来捕获定义异常通知的bean抛出的异常,并在抛出异常前执行相应的方法。
我想大家可能都在项目中有过这样的需求,想在某种异常抛出时进行一些记录操作,例如记录错误日志到数据库或日志文件中,但把这些代码分布到项目各处不但难于管理,并且代码量巨大,用Spring的AOP来实现拦截不失为一个比较好的方法。
下面,让我们来感受一下它的魅力吧。
1. 操作类TestBean
public class TestBean {
public void method1() throws Exception {
throw new Exception("Exception happened!");
}
/**
* 将字符串转换为整数.
*/
public int changeToNumber(String number) throws NumberFormatException {
// 当number为空或非数字时,将抛出NumberFormatException
int num = Integer.parseInt(number);
return num;
}
}
2. 错误日志拦截类ExceptionAdvisor
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");
}
}
}
运行ExceptionAdvisor类后,结果如下:
*************************************
Error happened in class: com.exception.TestBean
Error happened in method: method1
Exception class: java.lang.Exception
*************************************
Exception in method1 catch
*************************************
Error happened in class: com.exception.TestBean
Error happened in method: changeToNumber
args[0]: amigo
Exception class: java.lang.NumberFormatException
*************************************
Exception in changeToNumber catch