动态代理的使用以及其实现机制

动态代理可以提供对另一个对象的访问,同时隐藏实际对象的具体事实。代理一般会实现它所表示的实际对象的接口。代理可以访问实际对象,但是延迟实现实际对象的部分功能,实际对象实现系统的实际功能,代理对象对客户隐藏了实际对象。客户不知道它是与代理打交道还是与实际对象打交道。
 
   动态代理主要包含以下角色:
 
  动态代理类(以下简称代理类)是一个在创建类时在运行时指定的接口列表的类,该类具有下面的描述的行为。
  代理接口是代理类实现的一个接口。
  代理实例是代理类的一个实例。
 
  每个代理实例都有一个关联的调用处理程序对象,它可以实现接口InvocationHandler.通过其中一个代理接口的代理实例上的方法调用将被指派到实例的调用处理程序的Invoke方法。并传递代理实例、识别调用方法的java.lang,reflect.Method对象以及包含参数的Object类型的数组。调用处理程序以适当的方法处理编码的方法调用,并且它返回的结果将作为代理实例上方法调用的结果返回。
 
       目前Java开发包中包含了对动态代理的支持,但是其实现只支持对接口的实现,其实现主要通过java.lang.reflect.Proxy.类和java.lang.reflect.InvocationHandler接口。Proxy类主要用来获取动态代理对象,InvocationHandler接口用来约束调用者实现。
下面看一个例子:  
 代理接口:IComputer.java 
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 = "
";
        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();
        
    }

}

  

执行该程序的结果为:
start:1369121253400
Tank moving...
end:1369121260078
time:6678
 

动态生成的代理类的内容如下:

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();}
    }

}

  

看完这个例子就对动态代理实现机制应该有一定的了解了。
 
小结:动态代理在运行期间通过接口动态生成代理类,这为其带来了一定的灵活性,但这个灵活性却带来了两个问题,第一代理类必须要实现一个接口,如果没有实现接口会抛出一个异常。第二性能影响,因为动态代理使用反射的机制实现的,首先反射肯定比直接调用要慢,其次使用反射大量生成类文件可能引起Full GC造成性能影响,因为字节码文件加载后会存放JVM运行时区的方法区,(或者叫持久代)中,当方法区 满的时候,会引起Full GC,所以当你大量使用动态代理时,可以将持久代设置大一些,减少FUll GC次数。

  

原文地址:https://www.cnblogs.com/evolcq/p/3819245.html