设计模式—动态代理模式
动态代理在很多底层框架中都会用得到,比如struts,Spring等都用到了动态代理。
动态代理主要用来做方法的增强,让你可以在不修改源码的情况下,增强一些方法,在方法执行前后做任何你想做的事情,
因为在InvocationHandler的invoke方法中,你可以直接获取正在调用方法对应的Method对象,
具体应用的话,比如可以添加调用日志,做事务控制等。
所谓动态代理,就是指动态生成的代理,不需要我们编写,这样就可以解决这个代理类很多的问题,这样会极大地减少了我们的工作。
现在我们模拟jdk动态代理的实现:
1.处理方法接口 InvocationHandler.java
package com.bjsxt.proxy;
import java.lang.reflect.Method;
public interface InvocationHandler {
public void invoke(Object o, Method m);
}
2.处理方法实现(记录方法运行时间) TimeHandler.java
package com.bjsxt.proxy;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
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));
}
}
3.代理生成类 Proxy.java
package com.bjsxt.proxy;
import java.io.File;
import java.io.FileWriter;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import javax.tools.JavaCompiler;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
import javax.tools.JavaCompiler.CompilationTask;
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 +
" 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.bjsxt.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.bjsxt.proxy.InvocationHandler h;" + rt +
methodStr +
"}";
String fileName =
"d:/src/com/bjsxt/proxy/$Proxy1.java";
File f = new File(fileName);
FileWriter fw = new FileWriter(f);
fw.write(src);
fw.flush();
fw.close();
//use jdk compiler to 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
URL[] urls = new URL[] {new URL("file:/" + "d:/src/")};
URLClassLoader ul = new URLClassLoader(urls);
Class c = ul.loadClass("com.bjsxt.proxy.$Proxy1");
System.out.println(c);
Constructor ctr = c.getConstructor(InvocationHandler.class);
Object m = ctr.newInstance(h);
return m;
}
}
4.被代理类的接口 Moveable.java
package com.bjsxt.proxy;
public interface Moveable {
void move();
}
5. 被代理类 Tank.java
package com.bjsxt.proxy;
import java.util.Random;
public class Tank implements Moveable {
@Override
public void move() {
System.out.println("Tank Moving...");
try {
Thread.sleep(new Random().nextInt(10000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
6.客户端代码
package com.bjsxt.proxy;
public class Client {
public static void main(String[] args) throws Exception {
Tank t = new Tank();
InvocationHandler h = new TimeHandler(t);
Moveable m = (Moveable)Proxy.newProxyInstance(Moveable.class, h);
m.move();
}
}
//可以对任意的对象、任意的接口方法,实现任意的代理
在Proxy.newProxyInstance(Moveable.class, h)方法里头传入的是接口和处理方法类,有接口类就能获取接口的方法并调用;
有处理方法类InvocationHandler 就能指定对代理对象的处理方法。
生成的代理类代码 $Proxy1.java
package com.bjsxt.proxy;
import java.lang.reflect.Method;
public class $Proxy1 implements com.bjsxt.proxy.Moveable{
public $Proxy1(InvocationHandler h) {
this.h = h;
}
com.bjsxt.proxy.InvocationHandler h;
@Override
public void move() {
try {
Method md = com.bjsxt.proxy.Moveable.class.getMethod("move");
h.invoke(this, md);
}catch(Exception e) {e.printStackTrace();}
}}
具体更多细节可参考:Java-马士兵动态代理模式