Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > Java > 在Spring中,是如何实现 Aop 的,原理:动态代理+cglib 分步图解
【标  题】:在Spring中,是如何实现 Aop 的,原理:动态代理+cglib 分步图解
【关键字】:Spring,Aop,+cglib
【来  源】:http://www.blogjava.net/myao/archive/2006/04/26/aop.html

在Spring中,是如何实现 Aop 的,原理:动态代理+cglib 分步图解

Your Ad Here BlogJava - 不知道下一片树叶会飘到哪里 - 在Spring中,是如何实现 Aop 的,原理:动态代理+cglib 分步图解
posts - 19,  comments - 81,  trackbacks - 0

先把上文的连接给大家

将Spring推下神坛(仿造一个中国式Spring ,教大家一步一步从代码的角度理解 Ioc)
表示延续


然后我将谈谈本文到底要写什么东西。我们将从代码的角度去揭开AOP的面纱,这个姑娘真不差,

本帖子也是没有什么高深的东西,也许会继续刺痛那些拿概念吓唬人的高人,请继续飘过,谢谢


(五月花,是个大酒家,这里的XX真不差.....)不知道大家有没有看过这集 “综艺大哥大”


本文的目的是不要更改用户代码,把AOP的概念加到我的Dong里面去,还是先把目的写出来吧

也许着就是测试用例驱动开发的一种体现

1.在方法开始的时候输出 函数名 开始时间
2.在方法结束的时候输出 函数名 结束时间 以及 方法 花费 的 时间

然后再次贴出我的贴图,表明结果

aop01.JPG

成功完成。

这时候,也该看看我是如何完成的吧,请注意黑体字的内容

其他代码和上篇相同,这里添加了一个新的类

1.EventHandler.java
========================================================================
package org.dong.core;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Date;

public class EventHandler implements InvocationHandler {
???
??? private Object delegate;
???
??? public EventHandler(Object delegatec)
??? {
??? ??? //把需要代理的物件传进来
??? ??? this.delegate = delegatec;
??? }

???
??? public Object invoke(Object proxy, Method method, Object[] args)
??? ??? throws Throwable {
??? ???
??? ??? Object obj = null;
??? ???
??? ??? long startTime = System.currentTimeMillis();
??? ???
??? ??? //做之前 我可以加一些代码,这里就是功能实现的地方
??? ??? Date dt = new Date();
??? ??? System.err.println(method+"begin"+" @ "+dt.toString());
??? ???
??? ??? obj = method.invoke(delegate,args); //嘿修,我XX,代理的过程
??? ??? //做之后,我也可以加一些代码
??? ??? dt = new Date();
??? ??? long endTime = System.currentTimeMillis();
??? ???
??? ??? System.err.println(method+"end"+" @ "+
??? ??? ??? ??? dt.toString()+ "time "+(endTime - startTime)+" ms");
??? ??? return obj;//把做完的踢出去,嘿嘿
??? ??? ?
???
??? }

}

然后我修改了相应的BeanFactory实现类,来完成我们的功能

package org.dong.core;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;


public class XmlBeanFactory implements BeanFactory {

??? private String ResPath;

??? private static DongXmlOp XmlOp;

??? public XmlBeanFactory(Resource Res) {

??? ??? ResPath = Res.GetResPath();
??? ??? XmlOp = new DongXmlOp(ResPath);

??? }

??? public Object getBean(String string) {
??? ??? Bean tmpBean;
??? ??? tmpBean = XmlOp.GetBeanByID(string);
??? ??? try {
??? ???
??? ??? ??? Class rtnClass = Class.forName(tmpBean.getBeanClass());
??? ??? ??? //tempBean.getBeanClass()="user" or "userNew" in the DongContext.xml
??? ??? ???
??? ??? ??? //好了,在这里我们将实现 AOP,把我们到底干了什么给 print出来?*_*

????????????
??????????? Object rtnInstance= rtnClass.newInstance();
??? ??? ??? //这里是最重要的一句话,反射的精髓
??? ??? ???
??? ??? ??? //看到这里了么,动态代理被我们包括到了我们的Dong里面,完全没有改动用户代码哦。


??? ??? ??? InvocationHandler handler = new EventHandler(rtnInstance);

??? ??? ??? Object rtnInProxy=??? Proxy.newProxyInstance(
??? ??? ??? ??? ??? rtnInstance.getClass().getClassLoader(),
??? ??? ??? ??? ??? rtnInstance.getClass().getInterfaces(),
??? ??? ??? ??? ??? handler);
??? ??? ??? //动态代理的实现
??? ???
??? ??? ??? //记得我们原来直接就把 rtnInstance 踢出去了么,这次我们代理了以后把代理踢出去了。

??? ??? ??? return rtnInProxy;

??? ??? ???
??? ??? } catch (Exception e) {
??? ??? ??? System.err.println(e);
??? ??? }
??? ???

??? ??? return tmpBean;
??? }

}




posted on 2006-04-26 14:13 寒晴天 阅读(217) 评论(9)  编辑 收藏 收藏至365Key
【相关评论】
没有相关评论
【发表评论】
姓名:
邮件:
随机码*
评论*
      
|  首 页  |  版权声明  |  联系我们   |  网站地图  |
CopyRight © 2004-2007 bbb软讯网络 All Rigths Reserved.