JDK动态代理实现原理

来源:互联网 发布:时时彩助赢软件cpzyrj 编辑:程序博客网 时间:2024/06/05 14:31

之前虽然会用JDK的动态代理,但是有些问题却一直没有搞明白。比如说:InvocationHandler的invoke方法是由谁来调用的,代理对象是怎么生成的


  1. package dynamic.proxy;   
  2.   
  3. import java.lang.reflect.InvocationHandler;  
  4. import java.lang.reflect.Method;  
  5. import java.lang.reflect.Proxy;  
  6.   
  7. /** 
  8.  * 实现自己的InvocationHandler 
  9.  * @author zyb 
  10.  * @since 2012-8-9 
  11.  * 
  12.  */  
  13. public class MyInvocationHandler implements InvocationHandler {  
  14.       
  15.     // 目标对象   
  16.     private Object target;  
  17.       
  18.     /** 
  19.      * 构造方法 
  20.      * @param target 目标对象  
  21.      */  
  22.     public MyInvocationHandler(Object target) {  
  23.         super();  
  24.         this.target = target;  
  25.     }  
  26.   
  27.   
  28.     /** 
  29.      * 执行目标对象的方法 
  30.      */  
  31.     public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {  
  32.           
  33.         // 在目标对象的方法执行之前简单的打印一下  
  34.         System.out.println("------------------before------------------");  
  35.           
  36.         // 执行目标对象的方法  
  37.         Object result = method.invoke(target, args);  
  38.           
  39.         // 在目标对象的方法执行之后简单的打印一下  
  40.         System.out.println("-------------------after------------------");  
  41.           
  42.         return result;  
  43.     }  
  44.   
  45.     /** 
  46.      * 获取目标对象的代理对象 
  47.      * @return 代理对象 
  48.      */  
  49.     public Object getProxy() {  
  50.         return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),   
  51.                 target.getClass().getInterfaces(), this);  
  52.     }  
  53. }  
  54.   
  55. package dynamic.proxy;  
  56.   
  57. /** 
  58.  * 目标对象实现的接口,用JDK来生成代理对象一定要实现一个接口 
  59.  * @author zyb 
  60.  * @since 2012-8-9 
  61.  * 
  62.  */  
  63. public interface UserService {  
  64.   
  65.     /** 
  66.      * 目标方法  
  67.      */  
  68.     public abstract void add();  
  69.   
  70. }  
  71.   
  72. package dynamic.proxy;   
  73.   
  74. /** 
  75.  * 目标对象 
  76.  * @author zyb 
  77.  * @since 2012-8-9 
  78.  * 
  79.  */  
  80. public class UserServiceImpl implements UserService {  
  81.   
  82.     /* (non-Javadoc) 
  83.      * @see dynamic.proxy.UserService#add() 
  84.      */  
  85.     public void add() {  
  86.         System.out.println("--------------------add---------------");  
  87.     }  
  88. }  
  89.   
  90. package dynamic.proxy;   
  91.   
  92. import org.junit.Test;  
  93.   
  94. /** 
  95.  * 动态代理测试类 
  96.  * @author zyb 
  97.  * @since 2012-8-9 
  98.  * 
  99.  */  
  100. public class ProxyTest {  
  101.   
  102.     @Test  
  103.     public void testProxy() throws Throwable {  
  104.         // 实例化目标对象  
  105.         UserService userService = new UserServiceImpl();  
  106.           
  107.         // 实例化InvocationHandler  
  108.         MyInvocationHandler invocationHandler = new MyInvocationHandler(userService);  
  109.           
  110.         // 根据目标对象生成代理对象  
  111.         UserService proxy = (UserService) invocationHandler.getProxy();  
  112.           
  113.         // 调用代理对象的方法  
  114.         proxy.add();  
  115.           
  116.     }  
  117. }  




原创粉丝点击