java中Proxy类初探

  在java中提供了一个动态代理类,这个类位于java.lang.reflect包中的Proxy类中。什么是动态代理类呢?就是可以在运行时创建一个实现了一组给定接口的新类。听上去有点高深的样子,其实是提供了一种类的包装器,最终对接口中方法的调用还是由现有的接口的实现类去调用。

  比如,现在有一个ArrayList的对象,可以向其中添加任意的string对象,但是我们不需要添加apple这个字符串。ArrayList默认是不会提供这种字符串过滤的方法的,这个时候我们就可以使用Proxy代理类,在这个类中我们添加字符串的过滤规则,然后决定当前的字符串是否可以被添加到ArrayList中去。

  Proxy类中提供了一个方便的static方法用来构造proxy对象。Proxy.newProxyInstance(ClassLoader ,Class<?>[] interfaces, Invocationhandler).

  参数classLoader:类的加载器,使用null表示使用默认的加载器。

  参数 interfaces:需要代理的接口数组。

  invocationHandler:调用处理器,使用新建的proxy对象调用方法的时候,都会调用到该接口中的invoke方法。

 1 package com.app.myinterface;
 2 
 3 import java.lang.reflect.InvocationHandler;
 4 import java.lang.reflect.Method;
 5 import java.lang.reflect.Proxy;
 6 import java.util.ArrayList;
 7 import java.util.List;
 8 
 9 /**
10  * Created by Charles on 2015/11/2.
11  */
12 public class ProxyMotion {
13 
14     public static void main(String args[]) {
15 
16         ArrayList<String> content = new ArrayList<String>();
17         MyInvocationHandler handler = new MyInvocationHandler(content);
18         Object proxy = Proxy.newProxyInstance(null, new Class[]{List.class}, handler);
19         if(proxy instanceof List){  //判断当前的proxy对象是否是List接口
20             System.out.println("proxy is list");
21             List<String> mlist = (List<String>)proxy;
22             mlist.add("one");
23             mlist.add("two");
24             mlist.add("three");
25             mlist.add("apple");
26         }
27         System.out.println("proxy:"+proxy.toString());
28         System.out.println("content:"+content.toString());
29     }
30 
31 }
32 class MyInvocationHandler implements InvocationHandler {
33    //具体的调用类
34     Object target;
35 
36     public MyInvocationHandler(Object obj) {
37         target = obj;
38     }
39 
40     @Override
41     public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
42         //当前所持有的proxy对象
43         //method表示当前别调用的方法
44         //args表示方法中传递的参数
45         System.out.println("method name:"+method.getName());
46         if(method.getName().equals("add")){
47             if(args[0].equals("apple")){
48                 return false;
49             }
50         }
51         return method.invoke(target, args);
52     }
53 }

调用结果:

proxy is list
method name:add
method name:add
method name:add
method name:add
method name:toString
proxy:[one, two, three]
content:[one, two, three]


  这样,使用Proxy我们就轻松的为ArrayList添加了一个过滤规则。使用Proxy还有很多的好处,比如对于方法的调用堆栈等等。

原文地址:https://www.cnblogs.com/summerpxy/p/4929113.html