了解Java动态代理

动态代理的简要说明

在java的动态代理机制中,有两个重要的类或接口,一个是 InvocationHandler(Interface)、另一个则是 Proxy(Class)。

一、 InvocationHandler(interface)的描述:

InvocationHandler is the interface implemented by the invocation handler of a proxy instance.

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.
每一个动态代理类都必须要实现InvocationHandler这个接口,并且每个代理类的实例都关联到了一个handler,当我们通过代理对象调用 一个方法的时候,这个方法的调用就会被转发为由InvocationHandler这个接口的 invoke 方法来进行调用。我们来看看InvocationHandler这个接口的唯一一个方法 invoke 方法:
Object invoke(Object proxy, Method method, Object[] args) throws Throwable

这个方法接收三个参数和返回一个Object类型,它们分别代表的意思如下:

  • proxy:指代我们所代理的那个真实对象

  • method:指代的是我们所要调用真实对象的方法的Method对象

  • args:指代的是调用真实对象某个方法时接受的参数

返回的Object是指真实对象方法的返回类型,以上会在接下来的例子中加以深入理解。

the value to return from the method invocation on the proxy instance.

二、 Proxy(Class)的描述:

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这个方法:
public static Object newProxyInstance(ClassLoader loader, Class<?>[] interfaces,  InvocationHandler h)  throws IllegalArgumentException

 参数的理解:

// 一个ClassLoader对象,定义了由哪个ClassLoader对象来对生成的代理对象进行加载loader - the class loader to define the proxy class  // 一个Interface对象的数组,表示的是我将要给我需要代理的对象提供一组什么接口interfaces - the list of interfaces for the proxy class to implement// 一个InvocationHandler对象,表示的是当我这个动态代理对象在调用方法的时候,会关联到哪一个InvocationHandler对象上h - the invocation handler to dispatch method invocations to
 返回结果的理解:一个代理对象的实例
a proxy instance with the specified invocation handler of a proxy class that is defined by the specified class loader and that implements the specified interfaces

简单的Java代理

我们创建一个Java项目用于对动态代理的测试与理解,项目结构如下:

一、 先定义一个接口Interface,添加两个方法

package com.demo.proxy;

public interface Interface {
    void getMyName();

    String getNameById(String id);
}

二、 定义一个真实的实现上述接口的类,RealObject:

package com.demo.proxy;

public class RealObject implements Interface {
    @Override
    public void getMyName() {
        System.out.println("my name is demo");
    }

    @Override
    public String getNameById(String id) {
        System.out.println("argument id: " + id);
        return "demo";
    }
}

三、 定义一个代理对象,也实现了上述的Interface接口:

package com.demo.proxy;

public class SimpleProxy implements Interface {
    private Interface proxied;

    public SimpleProxy(Interface proxied) {
        this.proxied = proxied;
    }

    @Override
    public void getMyName() {
        System.out.println("proxy getmyname");
        proxied.getMyName();
    }

    @Override
    public String getNameById(String id) {
        System.out.println("proxy getnamebyid");
        return proxied.getNameById(id);
    }
}

四、 SimpleMain在Main方法中,测试上述的结果:

package com.demo.proxy;

public class SimpleMain {
    private static void consume(Interface iface) {
        iface.getMyName();
        String name = iface.getNameById("1");
        System.out.println("name: " + name);
    }

    public static void main(String[] args) {
        consume(new RealObject());
        System.out.println("========================================================");
        consume(new SimpleProxy(new RealObject()));
    }
}

五、 运行的结果如下:

my name is demoargument id: 1name: demo========================================================proxy getmynamemy name is demoproxy getnamebyidargument id: 1name: demo

Java的动态代理

完成了上述简单的Java代理,现在我们开始学习Java的动态代理,它比代理的思想更向前一步,因为它可以动态地创建代理并动态的处理对所代理方法的调用。

在动态代理上所做的所有调用都会被重定向到单一的调用处理器上,它的工作是揭示调用的类型并确定相应的对策。下面我们通过案例来加深Java动态代理的理解:

一、 创建一个继承了InvocationHandler的处理器:DynamicProxyHandler
 

package com.demo.dynamicproxy;

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

public class DynamicProxyHandler implements InvocationHandler {
    private Object proxied;

    public DynamicProxyHandler(Object proxied) {
        System.out.println("dynamic proxy handler constuctor: " + proxied.getClass());
        this.proxied = proxied;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("dynamic proxy name: " + proxy.getClass());
        System.out.println("method: " + method.getName());
        System.out.println("args: " + Arrays.toString(args));
        
        Object invokeObject = method.invoke(proxied, args);
        if (invokeObject != null) {
            System.out.println("invoke object: " + invokeObject.getClass());
        } else {
            System.out.println("invoke object is null");
        }
        return invokeObject;
    }
}

 二、 我们写一个测试的Main方法,DynamicProxyMain

 

三、 运行结果如下:

my name is demoargument id: 1name: demo==============================dynamic proxy handler constuctor: class com.demo.proxy.RealObjectin dynamicproxyMain proxy: class com.sun.proxy.$Proxy0dynamic proxy name: class com.sun.proxy.$Proxy0method: getMyNameargs: nullmy name is demoinvoke object is nulldynamic proxy name: class com.sun.proxy.$Proxy0method: getNameByIdargs: [1]argument id: 1invoke object: class java.lang.Stringname: demo

从以上输出结果,我们可以得出以下结论:

  • 与代理对象相关联的InvocationHandler,只有在代理对象调用方法时,才会执行它的invoke方法

  • invoke的三个参数的理解:Object proxy是代理的对象, Method method是真实对象中调用方法的Method类, Object[] args是真实对象中调用方法的参数 

Java动态代理的原理

一、 动态代理的关键代码就是Proxy.newProxyInstance(classLoader, interfaces, handler),我们跟进源代码看看:

public static Object newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h) throws IllegalArgumentException {
  // handler不能为空
    if (h == null) {
        throw new NullPointerException();
    }

    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.
     */
  // 通过loader和接口,得到代理的Class对象
    Class<?> cl = getProxyClass0(loader, intfs);

    /*
     * Invoke its constructor with the designated invocation handler.
     */
    try {
        final Constructor<?> cons = cl.getConstructor(constructorParams);
        final InvocationHandler ih = h;
        if (sm != null && ProxyAccessHelper.needsNewInstanceCheck(cl)) {
            // create proxy instance with doPrivilege as the proxy class may
            // implement non-public interfaces that requires a special permission
            return AccessController.doPrivileged(new PrivilegedAction<Object>() {
                public Object run() {
                    return newInstance(cons, ih);
                }
            });
        } else {
       // 创建代理对象的实例
            return newInstance(cons, ih);
        }
    } catch (NoSuchMethodException e) {
        throw new InternalError(e.toString());
    }
}

二、 我们看一下newInstance方法的源代码:

private static Object newInstance(Constructor<?> cons, InvocationHandler h) {
    try {
        return cons.newInstance(new Object[] {h} );
    } catch (IllegalAccessException | InstantiationException e) {
        throw new InternalError(e.toString());
    } catch (InvocationTargetException e) {
        Throwable t = e.getCause();
        if (t instanceof RuntimeException) {
            throw (RuntimeException) t;
        } else {
            throw new InternalError(t.toString());
        }
    }
} 

三、 当我们通过代理对象调用 一个方法的时候,这个方法的调用就会被转发为由InvocationHandler这个接口的 invoke 方法来进行调用。

体现这句话的代码,我在源码中没有找到,于是我在测试类的main方法中加入以下代码:

if (proxy instanceof Proxy) {
    InvocationHandler invocationHandler = Proxy.getInvocationHandler(proxy);
    invocationHandler.invoke(proxy, realObject.getClass().getMethod("getMyName"), null);
    System.out.println("--------------------------------------");
}

这段代码的输出结果如下,与上述中调用代理对象中的getMyName方法输出是一样的,不知道Jvm底层是否是这样判断的:

dynamic proxy handler constuctor: class com.demo.proxy.RealObjectdynamic proxy name: class com.sun.proxy.$Proxy0method: getMyNameargs: nullmy name is demoinvoke object is null
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值