Java Dynamic Proxy Learning (Proxy,invocationhandler)

Source: Internet
Author: User
A few days ago to see the dynamic proxy mechanism Java, do not know what the thing, and then looked. Anyway, I don't know.
The proxy of Invoke (Object Proxy, Method M, object[] args) is something that is used here. So the internet everywhere, to write their own understanding.
Java code   /**   *  Blind Date interface    *    *  @author  zhengt   *  @time  Jun 3, 2095 3:13:03 PM   */   public  interface xiangqininterface {       /**        *  Dating methods        */       public void  xiangqin ();  }  /**   *  John Blind Date implementation class    *    * @ author zhengt   *  @time  Jun 3, 2095 3:14:48 PM   */    public class zhangsanxiangqininterfaceimpl implements xiangqininterface {        public void xiangqin ()  {            system.out.println ("John to Blind Date, marry a beautiful wife." ");       }  }   import java.lang.reflect.invocationhandler;   import java.lang.reflect.method;      /**   *  Blind Date is a lifetime of great things, before the blind date to prepare, dress up more handsome.    *    *  @author  zhengt   *  @time  jun 3, 2095  3:15:48 PM   */   public class readyinvocationhandler implements  InvocationHandler {       //Blind Date Interface implementation class, that is, John Blind Date class         private Object zhangSan = null;           public readyinvocationhandler (object realsubject)  {            this.zhangSan = realSubject;       }           public object invoke (object proxy, method m ,  object[] args)  {           Object result = null;            try {                /**               *   Dynamic proxy class $proxy0 calls its own Xiangqin method when calling the Xiangqin method,                *  its own Xiangqin method calls the Super.h.invoke (this, , ), the invocation method of H of the parent proxy,                *  Which is the Invoke method of the Readyinvocationhandler class.                *  So, invoke (Object  proxy, method m, object[] args) Kind of proxy is actually a dynamic proxy class $proxy0,                *  If you turn it into a xiangqininterface and call its Xiangqin method, And then it's going to tuneWith Super.h.invoke (this, , ), this will kill the loop.                */                /**                *  the most problematic thing about this is what object proxy put here to do. I don't know that either.,               *  But at least we know what it is, what it's all about.                 */               system.out.println (Proxy.getclass (). Getsimplename ());                system.out.println ("John before the Blind date, the agent dressed him up." ");               result =  M.invoke (Zhangsan, args);      &NBsp;    } catch  (Exception ex)  {                system.exit (1);            }           return result;       }  }   import java.lang.reflect.invocationhandler;   import java.lang.reflect.method;   import java.lang.reflect.proxy;     / **   *  John came to the matchmaking agency (the scene of the Blind Date) and started a blind date.    *    *  @author  zhengt   *  @time  jun 3, 2095  3:17:16 PM   */   public class hunjiesuo {        public static void main (string args[])  {            //First will John Blind date of the implementation class instantiation, that is, get Xiangqininterface interfaceAn instance object          &NB
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.