Proxy动态代理

    Proxy动态代理

1 package com.test.dynamicproxy;
2 
3 public interface Subject {
4 
5     public void request();
6 }
 1 package com.test.dynamicproxy;
 2 
 3 public class RealSubject implements Subject {
 4 
 5     @Override
 6     public void request() {
 7         System.out.println("request() from RealSubject");
 8 
 9     }
10 
11 }

 

 1 package com.test.dynamicproxy;
 2 
 3 import java.lang.reflect.InvocationHandler;
 4 import java.lang.reflect.Method;
 5 
 6 public class SubjectInvocationHandler implements InvocationHandler {
 7 
 8     private Object object;
 9     
10     public SubjectInvocationHandler(Object obj) {
11         this.object = obj;
12     }
13     
14     @Override
15     public Object invoke(Object proxy, Method method, Object[] args)
16             throws Throwable {
17         method.invoke(object,args);
18         return null;
19     }
20 
21 }

 1 package com.test.dynamicproxy;
 2 
 3 import java.lang.reflect.InvocationHandler;
 4 import java.lang.reflect.Proxy;
 5 
 6 public class Client {
 7     
 8     public static void main(String[] args) {
 9         
10         RealSubject real = new RealSubject();
11         InvocationHandler ih = new SubjectInvocationHandler(real);
12         Class<?> clazz = real.getClass();
13         Subject s = (Subject)Proxy.newProxyInstance(clazz.getClassLoader(),clazz.getInterfaces(),ih);
14         
15         s.request();        //request() from RealSubject
16                 
17         System.out.println(s.getClass().getName());        //$Proxy0
18         System.out.println(s instanceof Proxy);        //true
19         
20         for(Class interf : s.getClass().getInterfaces()){
21             System.out.println(interf.getName());        //com.test.dynamicproxy.Subject
22         }
23         
24         
25 //        System.out.println(s.getClass().getPackage());         //null
26 //        System.out.println(s.getClass().getSuperclass());    //class java.lang.reflect.Proxy
27 //        System.out.println(Proxy.class.getPackage());     //package java.lang.reflect, Java Platform API Specification, version 1.6
28     }
29 
30 }

 

Object java.lang.reflect.Proxy.newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h) throws IllegalArgumentException

Returns an instance of a proxy class for the specified interfaces that dispatches method invocations to the specified invocation handler. This method is equivalent to:

     Proxy.getProxyClass(loader, interfaces).
         getConstructor(new Class[] { InvocationHandler.class }).
         newInstance(new Object[] { handler });
 

Proxy.newProxyInstance throws IllegalArgumentException for the same reasons that Proxy.getProxyClass does.

Parameters:
loader the class loader to define the proxy class
interfaces the list of interfaces for the proxy class to implement
h the invocation handler to dispatch method invocations to
Returns:
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
Throws:
IllegalArgumentException - if any of the restrictions on the parameters that may be passed to getProxyClass are violated
NullPointerException - if the interfaces array argument or any of its elements are null, or

 

 

 

 

 

 

 

Client 中   s   extends Proxy implements Subject

s.request() 方法调用流程 :

s 是 Subject 的 implementation  .s.request() 的实现

s 伪代码:

s extends Proxy implements Subject{

  InvocationHandler ih;

  ......

  public void request(){

    ih.invoke(....);

  }

 

}

 

posted @ 2015-05-08 14:02  java资料收集  阅读(217)  评论(0编辑  收藏  举报