Spring学习历程 --- AOP基础之代理

37 篇文章 0 订阅
29 篇文章 2 订阅

若是把代理没学,直接上AOP真的不容易,所以建议先学习代理。

先介绍静态代理

代理便是在一些情况下替代被代理的对象,实现一些任务。

比如说,有个 moveAble 接口(里面有move方法),有个Tank 实现了此接口,实现了move方法。

若是要记录move方法运行的时间,又要把move运行记录在日志里。

较好的方法是

创建一个代理类 TimeProxy ,它实现了moveAble接口, 它里面有一个属性m,其类型是moveAble接口 。

然后其调用move方法,在调用前后记录时间,并打印出来。

再创建一个LogProxy,它实现了moveAble接口,它里面有一个属性m,其类型是moveAble接口。

然后调用move方法,在调用前后记录日志,并打印出来。

等到需要先记录时间,后记录日志时,就先new TimeProxy , 再把 new 出来的对象,传入LogProxy类,然后再调用 LogProxy 对象。

就可以先记录时间后记录日志,若是想要先记录日志后记录时间,只需要先new 出LogProxy ,后将其传入TimeProxy 类中。

因为 TimeProxy 和 LogProxy  可以代理任意实现了moveAble接口的对象。

静态代理有美中不足之处,那就是当需要代理的类很多时,依然要为每个类写很多代理类,为了解决这一问题,便引来了动态代理。

然后再是动态代理

public class Test1 {
	public static void main(String[] args) throws Exception{
		String rt = "\r\n";
		String src = 
			"package com.bjsxt.proxy;" +  rt +
			"public class TankTimeProxy implements Moveable {" + rt +
			"    public TankTimeProxy(Moveable t) {" + rt +
			"        super();" + rt +
			"        this.t = t;" + rt +
			"    }" + rt +
			
			"    Moveable t;" + rt +
							
			"    @Override" + rt +
			"    public void move() {" + rt +
			"        long start = System.currentTimeMillis();" + rt +
			"        System.out.println(\"starttime:\" + start);" + rt +
			"        t.move();" + rt +
			"        long end = System.currentTimeMillis();" + rt +
			"        System.out.println(\"time:\" + (end-start));" + rt +
			"    }" + rt +
			"}";
		String fileName = System.getProperty("user.dir") 
							+ "/src/com/bjsxt/proxy/TankTimeProxy.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
		URL[] urls = new URL[] {new URL("file:/" + System.getProperty("user.dir") +"/src")};
		URLClassLoader ul = new URLClassLoader(urls);
		Class c = ul.loadClass("com.bjsxt.proxy.TankTimeProxy");
		System.out.println(c);
		
		Constructor ctr = c.getConstructor(Moveable.class);
		Moveable m = (Moveable)ctr.newInstance(new Tank());
		m.move();
		
	}
}
现将代理类的源码取出来,里面的代码可以改变。假设所有需要代理的类都实现了Moveable接口。
然后便可以不用写具体的代理类,直接动态编译一个,并用此类在内存中创造一个实例,再调用此代理实例的move方法,就可以检测不同的类的方法了。

若是要改变接口,便是需要传入接口进Proxy类中,便可代理实现其它接口的类了。代码如下:

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.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();
		
		//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);
		//m.move();

		return m;
	}
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值