《Pro Spring》学习笔记之AOP抛出通知
来源:互联网 发布:劲极太极服淘宝网 编辑:程序博客网 时间:2024/06/03 21:46
抛出同志在联结点之后运行(记住Spring所谓的联结点永远是方法调用),我们通过实现ThrowsAdvise接口来时先抛出通知,在实现ThrowsAdvise接口的方法中,有两个重载的afterThrowing方法/
public void afterThrowing(Exception ex) throws Throwable{
}
public void afterThrowing(Method method,Object[] obj,Object target,IllegalArgumentException ex) throws Throwable{
}
}
两个方法的调用规则如下:
(1)Spring只会运行一个afterThrowing方法
(2)当抛出Exception,运行第一个方法,抛出IllegalArgumentException ,运行第二个方法(以本文为例)/
(3)当一个参数合两个参数的抛出异常类型相同时,执行4个参数的方法
(4)Spring会选择签名与抛出异常最匹配的一个(同(2)所示)
示例代码:
WorkBean.java
package ch6.SimpleAOP.ExceptionAdvise;
public class WorkBean ...{
public void errorProneMethod() throws Exception...{
throw new Exception();
}
public void otherErrorProneMethod() throws IllegalArgumentException...{
throw new IllegalArgumentException();
}
}
public class WorkBean ...{
public void errorProneMethod() throws Exception...{
throw new Exception();
}
public void otherErrorProneMethod() throws IllegalArgumentException...{
throw new IllegalArgumentException();
}
}
ExceptionAdvise.java
package ch6.SimpleAOP.ExceptionAdvise;
import java.lang.reflect.Method;
import org.springframework.aop.ThrowsAdvice;
public class ExceptionAdvise implements ThrowsAdvice ...{
public void afterThrowing(Exception ex) throws Throwable...{
System.out.println("in exception advice");
}
public void afterThrowing(Method method,Object[] obj,Object target,IllegalArgumentException ex) throws Throwable...{
System.out.println("in IllegalAccessException advice");
}
}
import java.lang.reflect.Method;
import org.springframework.aop.ThrowsAdvice;
public class ExceptionAdvise implements ThrowsAdvice ...{
public void afterThrowing(Exception ex) throws Throwable...{
System.out.println("in exception advice");
}
public void afterThrowing(Method method,Object[] obj,Object target,IllegalArgumentException ex) throws Throwable...{
System.out.println("in IllegalAccessException advice");
}
}
测试代码:
package ch6.SimpleAOP.ExceptionAdvise;
import org.springframework.aop.framework.ProxyFactory;
public class TestExceptionAdvise ...{
/** *//**
* @param args
*/
public static void main(String[] args) ...{
WorkBean workBean=getWorkBean();
try ...{
workBean.errorProneMethod();
} catch (Exception e) ...{
}
try ...{
workBean.otherErrorProneMethod();
} catch (IllegalArgumentException e) ...{
}
}
public static WorkBean getWorkBean()...{
WorkBean target=new WorkBean();
ProxyFactory pf=new ProxyFactory();
pf.addAdvice(new ExceptionAdvise());
pf.setTarget(target);
return (WorkBean)pf.getProxy();
}
}
import org.springframework.aop.framework.ProxyFactory;
public class TestExceptionAdvise ...{
/** *//**
* @param args
*/
public static void main(String[] args) ...{
WorkBean workBean=getWorkBean();
try ...{
workBean.errorProneMethod();
} catch (Exception e) ...{
}
try ...{
workBean.otherErrorProneMethod();
} catch (IllegalArgumentException e) ...{
}
}
public static WorkBean getWorkBean()...{
WorkBean target=new WorkBean();
ProxyFactory pf=new ProxyFactory();
pf.addAdvice(new ExceptionAdvise());
pf.setTarget(target);
return (WorkBean)pf.getProxy();
}
}
运行结果:
in exception advice
in IllegalAccessException advice
- 《Pro Spring》学习笔记之AOP抛出通知
- 《Pro Spring》学习笔记之Automatic Proxying自动AOP代理实例
- Spring AOP学习笔记(3):AOP返回通知&异常通知&环绕通知
- Spring之AOP(学习笔记)
- 学习笔记之spring-AOP
- Spring学习笔记之AOP
- Spring学习笔记之AOP
- Spring AOP学习笔记(2):AOP前置通知&后置通知
- 《Pro Spring》学习笔记之集合注入
- 《Pro Spring》学习笔记之依赖解析
- 《Pro Spring》学习笔记之自动装配
- 《Pro Spring》学习笔记之依赖检查
- 《Pro Spring》学习笔记之InitializingBean
- 《Pro Spring》学习笔记之PropertyEditor
- 《Pro Spring》学习笔记之ApplicationContext国际化
- spring之AOP---后置通知
- Spring 学习笔记 (III) 之 Spring AOP
- Spring学习笔记之前置通知&后置通知
- 使用HttpContext的User属性来实现用户身份验证
- 智能ABC输入法的一个漏洞
- xf-calendar_plugin.js 日历完整代码
- 技术高手的十三个原则
- 动态规划算法的优化技巧(转贴)
- 《Pro Spring》学习笔记之AOP抛出通知
- 编写Unicode程序的六个准则
- Resin3+iis配置日记
- AJAX异步请求封装
- AT+CNMI指令详解
- Java 课堂记录 04 (文件和输入输出流)
- 中华人民共和国企业所得税法
- 测试了就写上来,没技术含量 addEventListener
- FreeBsd服务器一些相关资料整理