首先来谈为什么需要代理。
比如我有一个接口如下
- public interface Moveable {
- void move();
-
- }
它有一个move方法。现在我有这样一个需求,当我要通过子类实现该接口调用move方法的时候,我想在move方法的前后加一些逻辑
比如加日志、事务、权限等。我不想改变或者有时候我们没有源码没法改变move方法的时候,这时候我们可以让一个代理类帮我们做类似这样的事情。
因此我们先从静态代理说起。先建一个Moveable接口的实现类
- 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();
- }
-
- }
-
-
-
- }
然后我们再建一个代理类LogProxy
- public class LogProxy implements Moveable {
- private Moveable moveable;
- public LogProxy(Moveable moveable) {
- this.moveable = moveable;
- }
-
- @Override
- public void move() {
- System.out.println("Tank move begin......");
- moveable.move();
- System.out.println("Tank move end......");
-
- }
-
-
-
- }
让它实现Moveable接口,并且持有Tank类的引用也就是实现Moveable接口的子类。重写move方法,并在其前后加日志逻辑。这样一个静态代理类就写好了。下面我们看客户
端调用代码。
- public class Client {
- public static void main(String[] args) throws Exception {
- Tank t = new Tank();
- Moveable m = (Moveable)new LogProxy(t);
-
- m.move();
- }
- }
打印结果:
Tank move begin......
Tank Moving...
Tank move end......
静态代理是有缺陷的,如果我想给Tank加一个其他逻辑的代理,那么我需要再新建一个代理类,并实现Moveable接口。这样不够灵活也不够方便。我们想要这样一个代理,它可以代理实现任何接口的对象的任何方法。而不是像静态代理那样只能代理实现Moveable接口的move方法,那么下面我们就来看动态代理。
在分析动态代理之前我们来看一下JDK自带的动态代理如何使用,还是以上面的Moveable接口、Tank类为例。我们先建一个日志逻辑处理类LogHandler实现InvocationHandler接口。
- import java.lang.reflect.InvocationHandler;
- import java.lang.reflect.Method;
- public class LogHandler implements InvocationHandler {
- private Object target;
- public LogHandler(Object target) {
- this.target = target;
- }
- @override
- public Object invoke(Object proxy, Method method, Object[] args)
- throws Throwable {
-
- System.out.println("start......" );
-
- Object ret = null;
- try {
-
- ret = method.invoke(target, args);
-
- }catch(Exception e) {
- e.printStackTrace();
- throw e;
- }
- System.out.println("end.....");
- return ret;
- }
- }
然后客户端代码如下:
- import java.lang.reflect.Proxy;
- public class Client {
- public static void main(String[] args) {
-
- Moveable mgr = new Tank();
-
- LogHandler h = new LogHandler(mgr);
-
- Moveable m = Proxy.
- newProxyInstance(mgr.getClass().getClassLoader(), mgr.getClass().getInterfaces(), h);
- m.move();
- }
- }
这样加了日志逻辑的move方法运行结果如下:
start......
Tank Moving...
end......
下面我们来分析一下Proxy以及它的newProxyInstance(...)方法都干了些什么……
我借用尚学堂马士兵的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(ClassLoader loader,Class infce, InvocationHandler h) throws Exception {
- 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,new Object[] { null});" + 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();
-
-
- 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();
-
-
- 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;
- }
- }
需要注意的是这里的Proxy类和JDK中java.lang.reflect.Proxy并不相同。只是他们的原理大致相同,实现的细节并不同。JDK中生成$Proxy1并不是拼字符串,而是直接生成二进制码。