Java动态代理机制

来源:互联网 发布:数据漫游什么时候打开 编辑:程序博客网 时间:2024/06/17 20:41

在java的动态代理机制中,有两个重要的类或接口,一个是 InvocationHandler(Interface)、另一个则是 Proxy(Class),这一个类和接口是实现我们动态代理所必须用到的。

InvocationHandler:

InvocationHandler is the interface implemented by the invocation handler of a proxy instance. InvocationHandler是由代理实例的调用处理程序实现的接口。Each proxy instance has an associated invocation handler. When a method is invoked on a proxy instance, the method invocation is encoded and dispatched to the invoke method of its invocation handler.每个代理实例都有一个关联的调用处理程序。当在代理实例上调用方法时,方法调用将被编码并分派到其调用处理程序的invoke方法。

每一个动态代理类都必须要实现InvocationHandler这个接口,并且每个代理类的实例都关联到了一个handler,当我们通过代理对象调用一个方法的时候,这个方法的调用就会被转发为由InvocationHandler这个接口的invoke方法来进行调用。InvocationHandler这个接口的唯一一个方法invoke方法如下所示:

Object invoke(Object proxy, Method method, Object[] args) throws Throwableproxy:  指代我们所代理的那个真实对象method:  指代的是我们所要调用真实对象的某个方法的Method对象args:  指代的是调用真实对象某个方法时接受的参数

Proxy类

Proxy provides static methods for creating dynamic proxy classes and instances, and it is also the superclass of all dynamic proxy classes created by those methods. 代理提供静态方法来创建动态代理类和实例,它也是由这些方法创建的所有动态代理类的超类。

Proxy这个类的作用就是用来动态创建一个代理对象的类,它提供了许多的方法,但是我们用的最多的就是newProxyInstance这个方法:

//Returns an instance of a proxy class for the specified interfaces that dispatches method invocations to the specified invocation handler.//返回指定接口的代理类的实例,该接口将方法调用分派给指定的调用处理程序。public static Object newProxyInstance(ClassLoader loader, Class<?>[] interfaces,  InvocationHandler h)  throws IllegalArgumentException

这个方法的作用就是得到一个动态的代理对象,其接收三个参数,这三个参数所代表的含义如下:

/* * 通过Proxy的newProxyInstance方法来创建我们的代理对象 * 第一个参数 handler.getClass().getClassLoader() ,我们这里使用handler这个类的ClassLoader对象来加载我们的代理对象 * 第二个参数realSubject.getClass().getInterfaces(),我们这里为代理对象提供的接口是真实对象所实行的接口, * 表示我要代理的是该真实对象,这样我就能调用这组接口中的方法了 * 第三个参数handler, 我们这里将这个代理对象关联到了上方的 InvocationHandler 这个对象上 */public static Object newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h) throws IllegalArgumentException

示例代码

首先定义一个Subject类型的接口,为其声明了两个方法:

public interface Subject {    public void rent();    public void hello(String str);}

接着,定义了一个类来实现这个接口,这个类就是真实对象,RealSubject类:

public class RealSubject implements Subject {    @Override    public void rent() {        System.out.println("I want to rent my house");    }    @Override    public void hello(String str) {        System.out.println("hello: " + str);    }}

下一步,定义一个动态代理类,每一个动态代理类都必须要实现InvocationHandler这个接口:

public class DynamicProxy implements InvocationHandler{    //要代理的真实对象    public Object subject;    //构造方法,给要代理的真实对象赋值    public DynamicProxy(Object subject){        this.subject = subject;    }    @Override    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {        //在代理真实对象前添加一些自己的操作        System.out.println("before rent house");        System.out.println("Method:"+method);        //当代理对象调用真实对象的方法时,其会自动的跳转到代理对象关联的handler对象的invoke方法来进行调用        method.invoke(subject,args);        //在代理真实对象后也可以添加一些自己的操作        System.out.println("after rent house");        return null;    }}

最后,创建Client类用于测试:

public class Client {    public static void main(String[] args) {        //我们要代理的真实对象        Subject realSubject = new RealSubject();        //我们要代理哪个真实对象,就将该对象传进去,最后是通过该真实对象来调用其方法的        InvocationHandler handler = new DynamicProxy(realSubject);        /*         * 通过Proxy的newProxyInstance方法来创建我们的代理对象         * 第一个参数 handler.getClass().getClassLoader() ,我们这里使用handler这个类的ClassLoader对象来加载我们的代理对象         * 第二个参数realSubject.getClass().getInterfaces(),我们这里为代理对象提供的接口是真实对象所实行的接口,         * 表示我要代理的是该真实对象,这样我就能调用这组接口中的方法了         * 第三个参数handler, 我们这里将这个代理对象关联到了上方的 InvocationHandler 这个对象上         */        Subject subject = (Subject) Proxy.newProxyInstance(handler.getClass().getClassLoader(),                realSubject.getClass().getInterfaces(), handler);        System.out.println(subject.getClass().getName());        subject.rent();        subject.hello("world");    }}

控制台输出:

com.sun.proxy.$Proxy0before rent houseMethod:public abstract void Subject.rent()I want to rent my houseafter rent housebefore rent houseMethod:public abstract void Subject.hello(java.lang.String)hello: worldafter rent house

com.sun.proxy.$Proxy0是由System.out.println(subject.getClass().getName());这条语句打印出来的。为什么我们这里可以将其转化为Subject类型的对象?原因就是在newProxyInstance这个方法的第二个参数上,我们给这个代理对象提供了一组什么接口,那么我这个代理对象就会实现了这组接口,这个时候我们当然可以将这个代理对象强制类型转化为这组接口中的任意一个,因为这里的接口是Subject类型,所以就可以将其转化为Subject类型了。 
通过Proxy.newProxyInstance创建的代理对象是在jvm运行时动态生成的一个对象,它并不是我们的InvocationHandler类型,也不是我们定义的那组接口的类型,而是在运行是动态生成的一个对象,并且命名方式都是这样的形式,以$开头,proxy为中,最后一个数字表示对象的标号。

subject.rent();subject.hello("world");

这里是通过代理对象来调用实现的那种接口中的方法,这个时候程序就会跳转到由这个代理对象关联到的handler中的invoke方法去执行,而我们的这个handler对象又接受了一个RealSubject类型的参数,表示我要代理的就是这个真实对象,所以此时就会调用handler中的invoke方法去执行:

public Object invoke(Object object, Method method, Object[] args)            throws Throwable    {        //在代理真实对象前我们可以添加一些自己的操作        System.out.println("before rent house");        System.out.println("Method:" + method);        //当代理对象调用真实对象的方法时,其会自动的跳转到代理对象关联的handler对象的invoke方法来进行调用        method.invoke(subject, args);        //在代理真实对象后我们也可以添加一些自己的操作        System.out.println("after rent house");        return null;    }

在真正通过代理对象来调用真实对象的方法的时候,我们可以在该方法前后添加自己的一些操作,同时我们看到我们的这个 method 对象是这样的:

public abstract void com.shenjian.dynamicproxy.Subject.rent()public abstract void com.shenjian.dynamicproxy.Subject.hello(java.lang.String)

正好就是我们的Subject接口中的两个方法,这也就证明了当我通过代理对象来调用方法的时候,起实际就是委托由其关联到的handler对象的invoke方法中来调用,并不是自己来真实调用,而是通过代理的方式来调用的。