由微见著!模拟JDK动态代理的实现2---Spring AOP

来源:互联网 发布:淘宝不发货会自动退款 编辑:程序博客网 时间:2024/05/23 02:00

续:


/**
 * 测试端
 * @author Administrator
 *
 */
public class Client {
public static void main(String[] args) throws Exception {
UserMgr mgr = new UserMgrImpl();
InvocationHandler h = new TransactionHandler(mgr);
//TimeHandler h2 = new TimeHandler(h);
UserMgr u = (UserMgr)Proxy.newProxyInstance(UserMgr.class,h);
u.addUser();
}
}


-----------------------------------------------------------------------------------------------------------------------

public class TransactionHandler implements InvocationHandler {

private Object target;

public TransactionHandler(Object target) {
super();
this.target = target;
}


@Override
public void invoke(Object o, Method m) {
System.out.println("Transaction Start");
try {
m.invoke(target);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Transaction Commit");
}


}


-----------------------------------------------------------------------------------------------------------------------

public interface UserMgr {
void addUser();
}

-----------------------------------------------------------------------------------------------------------------------

public class UserMgrImpl implements UserMgr {


@Override
public void addUser() {
System.out.println("1: 插入记录到user表");
System.out.println("2: 做日志在另外一张表");
}

}


-----------------------------------------------------------------------------------------------------------------------

/**
 * 定义将要实现的中间代理类的统一接口
 * @author Rick
 *
 */
public interface InvocationHandler {
public void invoke(Object o, Method m);
}



-----------------------------------------------------------------------------------------------------------------------

public interface Moveable {
void move();

}


-----------------------------------------------------------------------------------------------------------------------


/**
 * 实现任意接口的类的代理-使用聚合代理
 * 代理模式有聚合代理和实现继承的代理
 * 继承的方式的缺点就是:n级代理就需要n级继承关系
 * @author Rick
 *
 */
public class Proxy {
public static Object newProxyInstance(Class infce, InvocationHandler h) throws Exception { //JDK6 Complier API, CGLib, ASM
String methodStr = "";
String rt = "\r\n";

Method[] methods = infce.getMethods();
/*
for(Method m : methods) {
methodStr += "@Override" + rt + 
"public void " + m.getName() + "() {" + rt +
"   long start = System.currentTimeMillis();" + rt +
"   System.out.println(\"starttime:\" + start);" + rt +
"   t." + m.getName() + "();" + rt +
"   long end = System.currentTimeMillis();" + rt +
"   System.out.println(\"time:\" + (end-start));" + rt +
"}";
}
*/
for(Method m : methods) {
methodStr += "@Override" + rt + 
"public void " + m.getName() + "() {" + rt +
"    try {" + rt +
"    Method md = " + infce.getName() + ".class.getMethod(\"" + m.getName() + "\");" + rt +
"    h.invoke(this, md);" + rt +
"    }catch(Exception e) {e.printStackTrace();}" + rt +

"}";
}

String src = 
"package com.tamao.dynamicAgent2.proxy;" +  rt +
"import java.lang.reflect.Method;" + rt +
"public class $Proxy1 implements " + infce.getName() + "{" + rt +
"    public $Proxy1(InvocationHandler h) {" + rt +
"        this.h = h;" + rt +
"    }" + rt +


"    com.tamao.dynamicAgent2.proxy.InvocationHandler h;" + rt +

methodStr +
"}";
String fileName = 
"d:/src/com/tamao/dynamicAgent2/proxy/$Proxy1.java";
File f = new File(fileName);
FileWriter fw = new FileWriter(f);
fw.write(src);
fw.flush();
fw.close();

//compile
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileMgr = compiler.getStandardFileManager(null, null, null);
Iterable units = fileMgr.getJavaFileObjects(fileName);
CompilationTask t = compiler.getTask(null, fileMgr, null, null, null, units);
t.call();
fileMgr.close();

//load into memory and create an instance
//加载到内存并生成一个实例--同样适用于网络load来的类
URL[] urls = new URL[] {new URL("file:/" + "d:/src/")};
URLClassLoader ul = new URLClassLoader(urls);
Class c = ul.loadClass("com.tamao.dynamicAgent2.proxy.$Proxy1");
System.out.println(c);

/**
* 站在JVM的角度,拿到类的构造方法的对象(PS:找到方法是根据参数来决定的)
*/

//找到参数类型是Moveable的参数类型的构造方法
Constructor ctr = c.getConstructor(InvocationHandler.class);
Object m = ctr.newInstance(h);
//m.move();


return m;
}
}


-----------------------------------------------------------------------------------------------------------------------

/**
 * 使用聚合来实现代理-代理和被代理也是实现Moveable(自定义Interface)
 * 本类是模拟代理的测试端
 * @author Rick
 *
 */


/**
 * 中间代理类/设定了被代理类为Object
 * @author Rick
 *
 */
public class TimeHandler implements InvocationHandler{

private Object target;






public TimeHandler(Object target) {
super();
this.target = target;
}


/**
* 定义代理逻辑
*/
@Override
public void invoke(Object o, Method m) {
long start = System.currentTimeMillis();
System.out.println("starttime:" + start);
System.out.println(o.getClass().getName());
try {
m.invoke(target);
} catch (Exception e) {
e.printStackTrace();
}
long end = System.currentTimeMillis();
System.out.println("time:" + (end-start));
}


}



阅读全文
0 0
原创粉丝点击