Java动态代理的原理

互联网 19-11-29

Java动态代理机制的出现,使得 Java 开发人员不用手工编写代理类,只要简单地指定一组接口及委托类对象,便能动态地获得代理类。(推荐: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这个类的作用就是用来动态创建一个代理对象。我们经常使用的是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.huhx.proxy;  public interface Interface {     void getMyName();      String getNameById(String id); }

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

package com.huhx.proxy;  public class RealObject implements Interface {     @Override     public void getMyName() {         System.out.println("my name is huhx");     }      @Override     public String getNameById(String id) {         System.out.println("argument id: " + id);         return "huhx";     } }

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

package com.huhx.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.huhx.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 huhx argument id: 1 name: huhx ======================================================== proxy getmyname my name is huhx proxy getnamebyid argument id: 1 name: huhx

Java的动态代理

完成了上述简单的Java代理,现在我们开始学习Java的动态代理,它比代理的思想更向前一步,因为它可以动态地创建代理并动态的处理对所代理方法的调用。在动态代理上所做的所有调用都会被重定向到单一的调用处理器上,它的工作是揭示调用的类型并确定相应的对策。下面我们通过案例来加深Java动态代理的理解:

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

package com.huhx.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:

package com.huhx.dynamicproxy;  import java.lang.reflect.InvocationHandler; import java.lang.reflect.Proxy;  import com.huhx.proxy.Interface; import com.huhx.proxy.RealObject;  public class DynamicProxyMain {     public static void consumer(Interface iface) {         iface.getMyName();         String name = iface.getNameById("1");         System.out.println("name: " + name);     }      public static void main(String[] args) throws Exception, SecurityException, Throwable {         RealObject realObject = new RealObject();         consumer(realObject);         System.out.println("==============================");          // 动态代理         ClassLoader classLoader = Interface.class.getClassLoader();         Class<?>[] interfaces = new Class[] { Interface.class };         InvocationHandler handler = new DynamicProxyHandler(realObject);         Interface proxy = (Interface) Proxy.newProxyInstance(classLoader, interfaces, handler);          System.out.println("in dynamicproxyMain proxy: " + proxy.getClass());         consumer(proxy);     } }

三、 运行结果如下:

my name is huhx argument id: 1 name: huhx ============================== dynamic proxy handler constuctor: class com.huhx.proxy.RealObject in dynamicproxyMain proxy: class com.sun.proxy.$Proxy0 dynamic proxy name: class com.sun.proxy.$Proxy0 method: getMyName args: null my name is huhx invoke object is null dynamic proxy name: class com.sun.proxy.$Proxy0 method: getNameById args: [1] argument id: 1 invoke object: class java.lang.String name: huhx

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

与代理对象相关联的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.huhx.proxy.RealObject dynamic proxy name: class com.sun.proxy.$Proxy0 method: getMyName args: null my name is huhx invoke object is null --------------------------------------

更多java知识请关注java基础教程栏目。

以上就是Java动态代理的原理的详细内容,更多内容请关注技术你好其它相关文章!

来源链接:
免责声明:
1.资讯内容不构成投资建议,投资者应独立决策并自行承担风险
2.本文版权归属原作所有,仅代表作者本人观点,不代表本站的观点或立场
标签: java
上一篇:php获取远程图片并下载保存到本地的方法分析 下一篇:phpmyadmin如何分配子账号

相关资讯