Java动态代理的理解

代码内容:

https://github.com/cjy513203427/Java_Advanced_Knowledge/tree/master/src/com/advance/dynamic_proxy

Subject接口

package com.advance.dynamic_proxy;
/**
 * Created by hasee on 2018/8/22.
 */
public interface Subject
{
    public void rent();

    public void hello(String str);
}

RealSubjec类t实现Subject

package com.advance.dynamic_proxy;/**
 * Created by hasee on 2018/8/22.
 */

/**
 * @Auther: 谷天乐
 * @Date: 2018/8/22 19:35
 * @Description:
 */
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);
    }
}

DynamicProxy实现InvocationHandler,必须要实现InvocationHandler类

package com.advance.dynamic_proxy;/**
 * Created by hasee on 2018/8/22.
 */

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * @Auther: 谷天乐
 * @Date: 2018/8/22 19:36
 * @Description:
 */
public class DynamicProxy implements InvocationHandler
{
    // 这个就是我们要代理的真实对象
    private Object subject;

    //    构造方法,给我们要代理的真实对象赋初值
    public DynamicProxy(Object subject)
    {
        this.subject = subject;
    }

    @Override
    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;
    }

}

启动类

package com.advance.dynamic_proxy;/**
 * Created by hasee on 2018/8/22.
 */

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

/**
 * @Auther: 谷天乐
 * @Date: 2018/8/22 19:36
 * @Description:
 */
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");
    }
}

原理:

通过代理类关联到InvocationHandler中的invoke方法调用了真实对象的方法,而不是直接调用的

我们可以对代理的方法前后自由的增加操作

先看InvocationHandler是什么东西

/**
 * {@code InvocationHandler} is the interface implemented by
 * the <i>invocation handler</i> of a proxy instance.
* InvocationHandler是一个被代理实例的调用程序实现的接口
* <p>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 {@code invoke} * method of its invocation handler.
* 每个代理实例都有相关联的调用处理程序
  当一个方法被代理实例调用时,调用方法会被编码并分派它的调用程序的调用方法
* @author Peter Jones * @see Proxy * @since 1.3
*/

再看InvocationHandler中唯一一个方法invoke

/**
     * Processes a method invocation on a proxy instance and returns
     * the result.  This method will be invoked on an invocation handler
     * when a method is invoked on a proxy instance that it is
     * associated with.
     *
     * @param   proxy the proxy instance that the method was invoked on
* 参数proxy是指方法被调用的代理实例

* @param method the {@code Method} instance corresponding to * the interface method invoked on the proxy instance. The declaring * class of the {@code Method} object will be the interface that * the method was declared in, which may be a superinterface of the * proxy interface that the proxy class inherits the method through.
* 参数method是一个实例,它就是调用在代理实例上的接口方法。声明的
    方法对象类是该方法声明的接口,这个接口是所有继承当前method的代理接口的父接口
* @param args an array of objects containing the values of the * arguments passed in the method invocation on the proxy instance, * or {@code null} if interface method takes no arguments. * Arguments of primitive types are wrapped in instances of the * appropriate primitive wrapper class, such as * {@code java.lang.Integer} or {@code java.lang.Boolean}.
* 参数args是包含了代理方法调用中传输的对象数组参数。
    或者这个接口没有参数。
    原始类型的参数被打包在合适的包装类中,如Integer或者Boolean
    意思就是int会包装成Integer...
*/

  public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable;
 

我们真正执行rent()和hello(String str)方法的地方在DynamicProxy类中

//当代理对象调用真实对象的方法时,其会自动的跳转到代理对象关联的handler对象的invoke方法来进行调用
        method.invoke(subject, args);

声明一个真实对象,InvocationHandler引用子类(在C++中接口实际就是一个父类,所以我这么说)传入真实对象

        //我们要代理的真实对象
        Subject realSubject = new RealSubject();

        //我们要代理哪个真实对象,就将该对象传进去,最后是通过该真实对象来调用其方法的
        InvocationHandler handler = new DynamicProxy(realSubject);

Proxy的newProxyInstance()方法

     * @param   loader the class loader to define the proxy class
    loader是加载类类加载定义代理类
* @param interfaces the list of interfaces for the proxy class * to implement

    interfaces是代理类实现的interface的集合
* @param h the invocation handler to dispatch method invocations to

    h是一个handler用来分派方法去调用
    @CallerSensitive
    public static Object newProxyInstance(ClassLoader loader,
                                          Class<?>[] interfaces,
                                          InvocationHandler h)
        throws IllegalArgumentException
    {
        Objects.requireNonNull(h);
     //在这里对我们的接口进行了复制
        final Class<?>[] intfs = interfaces.clone();
        final SecurityManager sm = System.getSecurityManager();
        if (sm != null) {
            checkProxyAccess(Reflection.getCallerClass(), loader, intfs);
        }

        /*
         * Look up or generate the designated proxy class.
         */
        Class<?> cl = getProxyClass0(loader, intfs);

        /*
         * Invoke its constructor with the designated invocation handler.
         */
        try {
            if (sm != null) {
                checkNewProxyPermission(Reflection.getCallerClass(), cl);
            }

            final Constructor<?> cons = cl.getConstructor(constructorParams);
            final InvocationHandler ih = h;
            if (!Modifier.isPublic(cl.getModifiers())) {
                AccessController.doPrivileged(new PrivilegedAction<Void>() {
                    public Void run() {
                        cons.setAccessible(true);
                        return null;
                    }
                });
            }
            return cons.newInstance(new Object[]{h});
        } catch (IllegalAccessException|InstantiationException e) {
            throw new InternalError(e.toString(), e);
        } catch (InvocationTargetException e) {
            Throwable t = e.getCause();
            if (t instanceof RuntimeException) {
                throw (RuntimeException) t;
            } else {
                throw new InternalError(t.toString(), t);
            }
        } catch (NoSuchMethodException e) {
            throw new InternalError(e.toString(), e);
        }
    }

控制台输出结果

第一个结果com.sun.proxy.$Proxy0System.out.println(subject.getClass().getName());

可以清晰地看到,我们用到了代理类,而不是自己定义的Subject类

com.sun.proxy.$Proxy0
before rent house
Method:public abstract void com.advance.dynamic_proxy.Subject.rent()
I want to rent my house
after rent house
before rent house
Method:public abstract void com.advance.dynamic_proxy.Subject.hello(java.lang.String)
hello: world
after rent house

总结:

代理是通过代理类关联到InvocationHandler中的invoke方法调用了真实对象的方法,从而完成了代理过程

原文地址:https://www.cnblogs.com/Java-Starter/p/9521740.html