标签:blog http java 使用 strong 文件
package com.proxy; public interface IComputer { void execute(); }
2.被代理的对象:Lattop.java
package com.proxy; //笔记本电脑 public class Laptop implements IComputer { public void execute() { System.out.println("电脑正在执行中......"); } }
3.调用处理类:TimeHander.java
package com.proxy; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; public class TimeHander implements InvocationHandler { private Object object; public TimeHander(Object object) { this.object = object; } public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { long start = System.currentTimeMillis(); System.out.println("start:"+start); method.invoke(object, args); Thread.sleep((int)(Math.random()*2000)); long end = System.currentTimeMillis(); System.out.println("end:"+end); System.out.println("total:"+(end-start)); return null; } }
4.测试程序
package com.proxy; import java.lang.reflect.Proxy; public class ProxyTest { public static void main(String[] args) { Laptop laptop = new Laptop(); TimeHander hander = new TimeHander(laptop); IComputer computer = (IComputer)Proxy.newProxyInstance(laptop.getClass().getClassLoader(), laptop.getClass().getInterfaces(), hander); computer.execute(); } }
二、动态代理运行机制
Proxy.newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h)
方法会返回一个代理对象类的实例。如上面例子中IComputer computer = (IComputer)Proxy.newProxyInstance(laptop.getClass().getClassLoader(), laptop.getClass().getInterfaces(), hander);执行这一句话的时候会通过反射机制动态的生成一个代理类,该类实现了IComputer接口,并且重写了接口里面的方法(也就是说代理类与被代理类有相同的接口),在该代理类里面有一个InvocationHandler类型的成员变量,也就是调用处理程序,通过调用处理程序来给被代理类增强功能。创建好代理类后就调用类加载器将该类加载到类存,然后再通过反射创建一个该代理类的实例对象。
1.代理接口:Moveable.java
package com.test; public interface Moveable { void move(); }
2.被代理对象:Tank.java
package com.test; import java.util.Random; public class Tank implements Moveable { public void move() { System.out.println("Tank moving..."); try { Thread.sleep(new Random().nextInt(10000)); } catch (InterruptedException e) { e.printStackTrace(); } } }
3.下面写一个Proxy类来为被代理对象产生一个代理类对象,来实现增加记录运行时间的功能。
package com.test; import java.io.File; import java.io.FileWriter; import java.lang.reflect.Constructor; import java.lang.reflect.Method; 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 interfaces,InvocationHandler h)throws Exception{ StringBuffer methodStr = new StringBuffer(); String tr = "\r\n"; Method[] methods = interfaces.getMethods(); //拼接代理类的方法 for (Method method : methods) { methodStr.append( " public "+ method.getReturnType()+ " " +method.getName()+"() {" + tr + " try {" + tr + " java.lang.reflect.Method md = " + interfaces.getName() + "." + "class.getMethod(\"" + method.getName() + "\");" + tr + " h.invoke(this,md);" + tr + " }catch(Exception e) {e.printStackTrace();}" + tr + " }" + tr ); } //拼接代理类 String src = "package com.test;" + tr + "import com.test.Moveable;" + tr + "public class TimeProxy implements " + interfaces.getName() + " {" + tr + " private com.test.InvocationHandler h;" + tr + " public TimeProxy(com.test.InvocationHandler h) {" + tr + " this.h = h;" + tr + " }" + tr + methodStr.toString() + tr + "}"; //创建代理类 String fileName = System.getProperty("user.dir") + "/src/com/test/TimeProxy.java"; File file = new File(fileName); FileWriter writer = new FileWriter(file); writer.write(src); writer.flush(); writer.close(); //编译 JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); StandardJavaFileManager fileMgr = compiler.getStandardFileManager(null, null, null); Iterable units = fileMgr.getJavaFileObjects(fileName); CompilationTask ct = compiler.getTask(null, fileMgr, null, null, null, units); ct.call(); fileMgr.close(); //加载类到内存: Class c = ClassLoader.getSystemClassLoader().loadClass("com.test.TimeProxy"); Constructor constructor = c.getConstructor(InvocationHandler.class); //得到参数为InvocationHandler类型的构造方法 Object m = constructor.newInstance(h); //通过该构造方法得到实例 return m; } }
4.TankProxy.java
package com.test; import java.lang.reflect.Method; public class TankProxy { public static <T> T getBean(final Object tank) throws Exception{ return (T)Proxy.newProxyInstance(tank.getClass().getInterfaces()[0], new InvocationHandler(){ public void invoke(Object proxy, Method method) { long start = System.currentTimeMillis(); System.out.println("start:"+start); try { method.invoke(tank, new Object[]{}); } catch (Exception e) { e.printStackTrace(); } long end = System.currentTimeMillis(); System.out.println("end:"+end); System.out.println("time:"+(end-start)); } }); } }
5.测试程序:
package com.test; import java.util.List; import com.extend.Tank2; import com.extend.Tank3; import com.juhe.LogProxy; import com.juhe.TimeProxy; public class Test { public static void main(String[] args) throws Exception { Tank tank = new Tank(); Moveable m = TankProxy.getBean(tank); m.move(); } }
动态生成的代理类的内容如下:
package com.test; import com.test.Moveable; public class TimeProxy implements com.test.Moveable { private com.test.InvocationHandler h; public TimeProxy(com.test.InvocationHandler h) { this.h = h; } public void move() { try { java.lang.reflect.Method md = com.test.Moveable.class.getMethod("move"); h.invoke(this,md); }catch(Exception e) {e.printStackTrace();} } }
标签:blog http java 使用 strong 文件
原文地址:http://www.cnblogs.com/evolcq/p/3819245.html