1 接口
public interface Pruduct { void selling(); }
?2 书籍类
public class Book implements Pruduct { @Override public void selling() { try { Thread.sleep(1000); System.out.println("books selling....."); } catch (InterruptedException e) { e.printStackTrace(); } } }
?3 定义日志类
public class LogTranService{ public static void before(){ System.out.println("begin log..."); } public static void after(){ System.out.println("finish log..."); } }
?4 定义时间类
public class TimeTranService { static long bgn; public static void before(){ bgn = System.currentTimeMillis(); System.out.println("begin time... " + bgn); } public static void after(){ long end = System.currentTimeMillis(); System.out.println("end time... " + (end-bgn)); } }
?5 定义InvocationHander?
import java.lang.reflect.Method; public interface InvocationHander { public void invoke(Object o,Method m); }
?
import java.lang.reflect.Method; public class ProxyHander implements InvocationHander { private Object target; public ProxyHander(Object target) { this.target = target; } @Override public void invoke(Object o, Method m) { try { TimeTranService.before(); LogTranService.before(); m.invoke(target); LogTranService.after(); TimeTranService.after(); } catch (Exception e) { e.printStackTrace(); } } }
?6 代理类
import java.io.File; import java.io.IOException; import java.lang.reflect.Constructor; import java.lang.reflect.Method; import javax.tools.JavaCompiler; import javax.tools.JavaCompiler.CompilationTask; import javax.tools.StandardJavaFileManager; import javax.tools.ToolProvider; import org.apache.commons.io.FileUtils; public class Proxy { /* * 空的构造函数 */ private Proxy(){ } /* * 返回代理类 */ public static Object newProxyInstance(Class inter,InvocationHander h){ String proxyClassName = "$Proxy3"; String packageName = inter.getPackage().getName(); String InHanderPackage = h.getClass().getPackage().getName(); String rt = "\r\n";// 换行 String methodCode = ""; for (Method method:inter.getMethods()) { methodCode+=" @Override"+rt+ " public void "+ method.getName()+"() {"+rt+ " try{"+rt+ " Method method = "+inter.getName()+".class.getMethod(\"" + method.getName()+ "\");"+rt+ " h.invoke(this,method); "+rt+ " }catch(Exception e ){" +rt+ " e.printStackTrace();" +rt+ " }"+rt+ " }"; } /* * 总的java代码 */ String javaCode= "package "+packageName+";"+rt+ "import "+InHanderPackage+".InvocationHander;"+rt+ "import java.lang.reflect.Method;"+rt+ "public class "+proxyClassName+" implements "+inter.getName()+" {"+rt+ " public "+proxyClassName+"("+InHanderPackage+".InvocationHander h) {"+rt+ " super();"+rt+ " this.h = h;"+rt+ " }"+rt+ " private "+InHanderPackage+".InvocationHander h;"+rt+ methodCode+rt+ "}"; /* * 生成java文件 */ // 生成文件路径 String filename = System.getProperty("user.dir")+"/bin/"+packageName+"/"+proxyClassName+".java"; File file = new File(filename); try { // 需要commons-io的jar方便的操作文件 FileUtils.writeStringToFile(file, javaCode); } catch (IOException e) { e.printStackTrace(); } // 编译 拿到编译器 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); // call进行编译 t.call(); try { fileMgr.close(); } catch (IOException e) { e.printStackTrace(); } // load到内存 ClassLoader cl = ClassLoader.getSystemClassLoader(); try { Class c = cl.loadClass(packageName+"."+proxyClassName); Constructor ctr = c.getConstructor(InvocationHander.class); System.out.println("代理类的名字为:"+c.getName()+"==========="); return ctr.newInstance(h); } catch (Exception e) { e.printStackTrace(); } return null; } }
?7 测试
public class Client { public static void main(String[] args) { Book book = new Book(); InvocationHander h = new ProxyHander(book); Pruduct m = (Pruduct)Proxy.newProxyInstance(Pruduct.class,h); m.selling(); } }
?
?
原文:http://hangzhoujava.iteye.com/blog/2268400