java动态代理

来源:互联网 发布:linux终止当前命令 编辑:程序博客网 时间:2024/06/06 01:14

纸上得来终觉浅

上次学习和了解了java反射机制,理解动态代理就简单些了。要实现动态代理,有两个重要的类或接口:

1.一个是 InvocationHandler(Interface),其中有唯一一个invoke方法:

Object invoke(Object proxy, Method method, Object[] args) throws Throwableproxy:  指代我们所代理的那个真实对象method:  指代的是我们所要调用真实对象的某个方法的Method对象args:  指代的是调用真实对象某个方法时接受的参数
另一个是Proxy(Class),它的作用是用来动态创建一个代理对象的类,它提供了许多的方法,但是我们用的最多的就是 newProxyInstance 这个方法:

public static Object newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h) throws IllegalArgumentExceptionloader:  一个ClassLoader对象,定义了由哪个ClassLoader对象来对生成的代理对象进行加载interfaces:  一个Interface对象的数组,表示的是我将要给我需要代理的对象提供一组什么接口,如果我提供了一组接口给它,那么这个代理对象就宣称实现了该接口(多态),这样我就能调用这组接口中的方法了h:  一个InvocationHandler对象,表示的是当我这个动态代理对象在调用方法的时候,会关联到哪一个InvocationHandler对象上

2.下面一个具体的示例:

public interface count {public int add(int a,int b);public int div(int a,int b);}
public class mathCount implements count{public int add(int a,int b){return  a+b;}public int div(int a,int b){return  a/b;}}
import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;public class dyproxy implements InvocationHandler{private Object subject;public dyproxy(Object subject){this.subject = subject;}/*当代理对象调用真实对象的方法时,会自动跳转到其关联的handler对象的invoke方法,就是下面的这个方法*/public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {/*在执行代理对象的方法前,添加天自己的操作*/System.out.println("before method:");System.out.println("the method:"+method);try {/*以反射的方式调用真实对象中的方法*/method.invoke(subject, args);} catch (Exception e) {e.printStackTrace();/*出现异常打印信息*/System.out.println("exception:");}/*在执行代理对象的方法后,添加自己的操作*/System.out.println("after method:");return 1;}}
import java.lang.reflect.InvocationHandler;import java.lang.reflect.Proxy;public class Main {public static void main(String[] args) {/*建立要代理的真实对象*/count realcount = new mathCount();/*我们要代理哪个真实对象,就将该对象传进去,最后是通过该真实对象来调用其方法的*/InvocationHandler handler = new dyproxy(realcount);/*通过Proxy的newProxyInstance来创建我们的代理对象 * 第一个参数 handler.getClass().getClassLoader() ,我们这里使用handler这个类的 * ClassLoader对象来加载我们的代理对象 * 第二个参数realSubject.getClass().getInterfaces(),我们这里为代理对象提供的 * 接口是真实对象所实行的接口,表示我要代理的是该真实对象,这样我就能调用这组接口中的方法了 * 第三个参数handler, 我们这里将这个代理对象关联到了上方的 InvocationHandler 这个对象上 */count count = (count)Proxy.newProxyInstance(handler.getClass().getClassLoader(),realcount.getClass().getInterfaces(), handler);count.add(1, 2);count.div(3, 0);}}
运行结果如下:

before method:the method:public abstract int com.roadArchitectWeb.dyproxy.count.add(int,int)after method:before method:the method:public abstract int com.roadArchitectWeb.dyproxy.count.div(int,int)java.lang.reflect.InvocationTargetExceptionat sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:497)at com.roadArchitectWeb.dyproxy.dyproxy.invoke(dyproxy.java:18)at com.sun.proxy.$Proxy0.div(Unknown Source)at com.roadArchitectWeb.dyproxy.Main.main(Main.java:25)Caused by: java.lang.ArithmeticException: / by zeroat com.roadArchitectWeb.dyproxy.mathCount.div(mathCount.java:8)... 7 moreexception:after method:

上面基本说明了动态代理的用法。

3.静态代理

与动态代理相对的是静态代理,静态代理实际上就是新建一个类,对要代理的类进行引用,对其进行封装。如果有很多个要代理的类,就要新建很多个类。所以需要用到动态代理。原理很简单,不需要代码进行演示。

这篇简要概述了动态代理,后面还有一篇进行了详细的总结:http://blog.csdn.net/jintao_ma/article/details/52788923

0 0
原创粉丝点击