笑看尘世|Spring4基础二二-AOP篇-通知(2)-通知详解( 二 )

public void afterThrowing(Method method, Object[] args, Object target, Exception ex)public void afterThrowing(Method method, Object[] args, Object target, ServletException ex)自定义异常
//运算异常public class MathException extends Exception{public MathException(String msg) {super(msg);}}异常通知
//异常通知public class MyThrowsAdvice implements ThrowsAdvice{public void afterThrowing(Exception ex){if(ex instanceof MathException){System.out.println("捕获...MathException异常");}}}

  • 6.xml配置
beforeAdviceafterReturningAdvicemethodInterceptorthrowsAdvice
  • 7.测试
@Testpublic void test1() throws MathException{ClassPathXmlApplicationContext application = new ClassPathXmlApplicationContext("applicationContext.xml");//获取代理对象IServiceDemo service = (IServiceDemo)application.getBean("serviceProxy");String str = service.doWork("doWork 。。。");//正常测试//String str1 = service.doWork("1");//异常测试System.out.println(str);application.close();//销毁}
    • 正常测试结果
前置的通知-目标方法执行之前:[MethodBeforeAdvice]环绕通知[MethodInterceptor]:目标方法执行之前环绕通知[MethodInterceptor]:目标方法执行之后后置通知-目标方法执行之后:[AfterReturningAdvice]执行完毕--结果DOWORK 。。。
    • 异常测试结果打印
前置的通知-目标方法执行之前:[MethodBeforeAdvice]环绕通知[MethodInterceptor]:目标方法执行之前异常通知[ThrowsAdvice]-捕获...MathException异常
    • 异常测试结果控制台报错
【笑看尘世|Spring4基础二二-AOP篇-通知(2)-通知详解】com.spring.aop.advice.MathException: MathException异常出现了......at com.spring.aop.demo1.ServiceDemoImpl.doWork(ServiceDemoImpl.java:11)
笑看尘世|Spring4基础二二-AOP篇-通知(2)-通知详解