本站首页    管理页面    写新日志    退出


«October 2025»
1234
567891011
12131415161718
19202122232425
262728293031


公告

戒除浮躁,读好书,交益友


我的分类(专题)

日志更新

最新评论

留言板

链接

Blog信息
blog名称:邢红瑞的blog
日志总数:523
评论数量:1142
留言数量:0
访问次数:9719995
建立时间:2004年12月20日




[java语言]深入浅出 spring AOP (一)
原创空间,  软件技术

邢红瑞 发表于 2005/11/13 14:09:02

先不讨论AOP的各种名词,也不作各种AOP的比较,我将在例子中介绍各种名词。1。先写一个javabean,就是target object。package org.tatan.test; public class MessageBean {  public void write() {         System.out.print("AOP example");     }}2。写一个AOP的advice类MethodInterceptor是AOP联盟的标准接口,它是最简单最实用的连接点(joinpoint),实现了around advice ,你可以在他返回前调用target的方法。package org.tatan.test;import org.aopalliance.intercept.MethodInterceptor;import org.aopalliance.intercept.MethodInvocation;public class MessageCode implements MethodInterceptor {     public Object invoke(MethodInvocation invocation) throws Throwable {        System.out.print("this is a ");        Object returnValue = invocation.proceed();                return returnValue ;    } }3。把MessageCode advice weave 到proxy factory,proxy factory是整个架构的核心先创建instance of MessageBean,然后创建代理的instance ,MessageCode advice 传递给的addAdvice()方法,设置Target Object,调用getProxy()产生代理对象。import org.springframework.aop.framework.ProxyFactory; public class AOPExample {  public static void main(String[] args) {         MessageBean target = new MessageBean();                           ProxyFactory pf = new ProxyFactory();              pf.addAdvice(new MessageCode());         pf.setTarget(target);              MessageBean proxy = (MessageBean) pf.getProxy();                  //输出原始信息         target.write();        //输出代理对象调用的信息         proxy.write();     } }4。classpath中加入cglib-nodep-2.1_2.jar ,spring.jar,aopalliance.jar,commons-logging.jar结果AOP examplethis is a AOP example


阅读全文(7440) | 回复(1) | 编辑 | 精华
 


回复:深入浅出 spring AOP (一)
原创空间,  软件技术

rong(游客)发表评论于2006/6/29 14:25:23

谢谢,你写的很好!  


个人主页 | 引用回复 | 主人回复 | 返回 | 编辑 | 删除
 


» 1 »

发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.762 second(s), page refreshed 144793253 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号