1. SubJect.class 
  2.  
  3. /** 
  4.  * Created by IntelliJ IDEA. 
  5.  * User: Administrator 
  6.  * Date: 2011-11-10 
  7.  * Time: 15:07:40 
  8.  * To change this template use File | Settings | File Templates. 
  9.  */ 
  10. public interface SubJect { 
  11.     public void info(); 

 


  
  1. RealSubJect.class 
  2. /** 
  3.  * Created by IntelliJ IDEA. 
  4.  * User: Administrator 
  5.  * Date: 2011-11-10 
  6.  * Time: 15:08:33 
  7.  * To change this template use File | Settings | File Templates. 
  8.  */ 
  9. public class RealSubJect implements SubJect{ 
  10.     @Override 
  11.     public void info(){ 
  12.         System.out.println("这是真实的自我"); 
  13.     } 

 


  
  1. /** 
  2.  * Created by IntelliJ IDEA. 
  3.  * User: Administrator 
  4.  * Date: 2011-11-10 
  5.  * Time: 15:11:26 
  6.  * To change this template use File | Settings | File Templates. 
  7.  */ 
  8. public class ProxySubJect implements InvocationHandler { 
  9.     private Object inObj; 
  10.     public ProxySubJect(Object obj){ 
  11.         this.inObj=obj; 
  12.     } 
  13.     @Override 
  14.     public Object invoke(Object proxy, Method method, Object[] args) 
  15.             throws Throwable { 
  16.         // TODO Auto-generated method stub 
  17.         method.invoke(inObj, args); 
  18.  
  19.         return null
  20.     } 

 


  
  1. /** 
  2.  * Created by IntelliJ IDEA. 
  3.  * User: Administrator 
  4.  * Date: 2011-11-10 
  5.  * Time: 15:15:35 
  6.  * To change this template use File | Settings | File Templates. 
  7.  */ 
  8. public class Test { 
  9.     public static void main(String[] args){ 
  10.         RealSubJect real=new RealSubJect(); 
  11.         ProxySubJect proxy = new ProxySubJect(real); 
  12.  
  13.         SubJect subject=(SubJect)Proxy.newProxyInstance(proxy.getClass().getClassLoader(),real.getClass().getInterfaces(),proxy) ; 
  14.         subject.info(); 
  15.     }