Dynamic agent mechanism of Java

Source: Internet
Author: User

A few days ago to see the dynamic agent mechanism of Java, do not know what the thing, and then looked at. I don't know, anyway.

What is the proxy for invoke (Object Proxy, Method m, object[] args), and what is it for? So I searched the internet and wrote my own understanding.

/*** Blind Date interface * *@authorZhengt * @time June 3, 2095 3:13:03 PM*/ Public InterfaceXiangqininterface {/*** Method of Blind Date*/     Public voidXiangqin ();}/*** Zhang San Blind Date Implementation class * *@authorZhengt * @time June 3, 2095 3:14:48 PM*/ Public classZhangsanxiangqininterfaceimplImplementsXiangqininterface { Public voidXiangqin () {System.out.println ("Zhang San to a blind date and marry a beautiful wife." "); }}ImportJava.lang.reflect.InvocationHandler;ImportJava.lang.reflect.Method;/*** Blind Date is a lifelong event, before a blind date to prepare, dressed up handsome. *  * @authorZhengt * @time June 3, 2095 3:15:48 PM*/ Public classReadyinvocationhandlerImplementsInvocationhandler {//the realization class of the Blind Date interface, that is, Zhang San Blind Date class    PrivateObject Zhangsan =NULL;  PublicReadyinvocationhandler (Object realsubject) { This. Zhangsan =Realsubject; }     Publicobject Invoke (object proxy, Method m, object[] args) {object result=NULL; Try {            /*** The dynamic proxy class $proxy0 calls its own Xiangqin method when calling the Xiangqin method, while its own Xiangqin method calls the Super.h.invoke (this,,)             , that is, the Invoke method of the parent proxy's H, * that is, the Invoke method of the Readyinvocationhandler class. * So, the proxy of Invoke (Object Proxy, Method m, object[] args) is actually a dynamic proxy class $proxy0, * if you turn it into Xiangqininterface and then call its X             Iangqin method, then it will call Super.h.invoke (this,,), so it will die loop. */            /*** Online about the most problem here is the object proxy put here to do what? I don't know about that, but at least we know what it is, and what it does, it's unclear.*/System.out.println (Proxy.getclass (). Getsimplename ()); System.out.println ("Before the Zhang San Blind date, the agent dressed him up." "); Result=M.invoke (Zhangsan, args); } Catch(Exception ex) {System.exit (1); }        returnresult; }}ImportJava.lang.reflect.InvocationHandler;ImportJava.lang.reflect.Method;ImportJava.lang.reflect.Proxy;/*** Zhang San came to the dating agency (blind Spot) and began a blind date. *  * @authorZhengt * @time June 3, 2095 3:17:16 PM*/ Public classHunjiesuo { Public Static voidMain (String args[]) {//first, the Zhang San Blind Date implementation class instantiation, that is, to get an instance of the Xiangqininterface interface objectXiangqininterface Zhangsan =NewZhangsanxiangqininterfaceimpl (); /*** Get Zhangsanxiangqininterfaceimpl a proxy class for this class, and bind a handler class Readyinvocationhandler for the proxy class. * Listen very much to the mouth, in fact, each time you call Zhangsanxiangqininterfaceimpl this subclass of the Xiangqin method, * is not zhangsan this Zhangsanxiangqininterfaceimpl class instance to call , * But this Zhangsanxiangqininterfaceimpl proxy class Readyinvocationhandler to invoke its own Invoke method, * This invoke method can call Zhangsan The Xiangqin method of this example*/        /*** How to implement dynamic Proxy in Java? * First step, we have an interface, there is an interface implementation class, and this implementation class is what we want to proxy object, * the so-called proxy is called when the implementation of the method of the class, you can before the method execution         After doing extra work, this is the agent. * The second step is to write ourselves a class that can do extra work when the method of the proxy class executes, and this class must inherit the Invocationhandler interface, * Why inherit it?         Because an instance of the proxy class does not tune the actual implementation of this method of the class when invoking the method of implementing the class, it instead calls the Invoke method of the class (the method that must be implemented when inheriting), in which you can invoke the method of the real implementation class.         * The third step, when you want to use the proxy class instance to invoke the implementation of the method of the class, write down the following two pieces of code. */Xiangqininterface Proxy=(Xiangqininterface) proxy.newproxyinstance (Zhangsan.getclass () getClassLoader (), Zhang San.getclass (). Getinterfaces (),NewReadyinvocationhandler (Zhangsan));        Proxy.xiangqin (); /*** Here to explain the meaning of the long-length code in the middle, and what exactly did it work?         * First, according to Zhangsan.getclass (). getClassLoader () This class loader and * Zhangsan.getclass () to proxy classes. Getinterfaces () all interfaces implemented by the proxy class * The method of calling Proxy.getproxyclass (ClassLoader loader, class<?> ... interfaces) * As a parameter returns the Java.lang.Class object of the proxy class, which is the         Java dynamically generated proxy classes $proxy0 class objects.         * At the same time, Java also allows the dynamically generated $PROXY0 class to implement all interfaces for the implementation of the proxy class, and inherits the proxy interface. * Second, instantiate an instance of this dynamically generated $proxy0 class, instantiate the proxy class constructor as proxy (Invocationhandler h), * that is, to instantiate the dynamically generated $proxy0 class, you must give it a Invocationhan         The Dler parameter, which is the class readyinvocationhandler that we implement to do extra work before and after the proxy class * method is executed. * This Code proxy.newproxyinstance (Zhangsan.getclass (). getClassLoader (), Zhangsan.getclass (). Getinterfaces (), New         Readyinvocationhandler (Zhangsan)) * Obtained is actually a class called $proxy0 extends Proxy implements Xiangqininterface class.         * Third, the $proxy0 class is forced into the xiangqininterface type, calling the Xiangqin method. */    }}

Transferred from: http://www.iteye.com/topic/683613

Dynamic agent mechanism of Java

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.