Dark Horse programmer--java High-Tech (10) _ Dynamic agent and AOP programming

Source: Internet
Author: User

------Java Training, Android training, iOS training,. NET training, look forward to communicating with you! -------

One, the dynamic core

Dynamic agent of the two core, proxy and Invocationhandler, are the interface class under the Java.lang.reflect package, it can be seen that dynamic proxy is a high-level application of reflection!

How is the relationship between agent and reflection reflected? The method of building the agent and its parameters can be seen:

1, the only way to broker the core of handler is the core parameter of the Invoke () method, which is a class type, and when the proxy is called, invoke to the target object to return retval.

2, the constructor method parameter of the agent, that is, the first two parameters of the Newproxyinstance () method of the Ox x, is the target interface of the ClassLoader and interface[], but also through the interface class object reflected get to;

Second, the proxy invocation method

Object Invoke (Object proxy, method method,object[] args) method, which processes the method call on the proxy instance and returns the result.

Each proxy instance has an associated call handler. When a method is called on a proxy instance, the method call is encoded and assigned to the Invoke method of its calling handler.

The meaning of three parameters is:

The proxy instance on which the Object proxy invokes the method

Method method corresponds to an instance of method of an interface called on a proxy instance

Object[] args contains an array of objects for the parameter values of the method calls on the incoming proxy instance, or null if the interface method does not use parameters

Third, the creation of the proxy instance

Method one. 1, create a class file of several interfaces through the Proxy.getproxyclass () method,

2, the construction method can be obtained by class file.

3, finally the proxy instance is obtained by the Newinstance method of constructing the method instance.

Method Two., through the proxy's Newproxyinstance () method, directly returns a proxy instance with the specified invocation handler of the proxy class.

It is defined by the specified class loader and implements the specified interface

The parameter list for this method is:

ClassLoader loader, specifying a class loader to load the specified interface

Class<?>[] interfaces, giving the interface that needs to create the proxy instance

Invocationhandlerh, the interface that gives the invocation handler implementation of the proxy instance

It is obvious that the second method is to re-encapsulate the code of the proxy logic, which is helpful to write and manage the three major agent construction parameters, so that we can concentrate on processing the Invoke () method, target object, slice information (Advice), realize AOP programming;

The following is the establishment of the proxy instance, and the application of further AOP programming:

  static void Clazzproxy () throws exception{ClassclazzProxy1 = Proxy.getproxyclass (Collection.class.getClassLoad           ER (), collection.class);                     System.out.println (Clazzproxy1.getname ());           System.out.println ("---------------constructorslist--------------");           Constructor[]constructors = Clazzproxy1.getconstructors ();                  for (constructorconstructor:constructors) {stringname = Constructor.getname ();                  Stringbuildersbuilder = new StringBuilder (name);                  Sbuilder.append (' (');                  Class[]clazzparams = Constructor.getparametertypes ();                  for (Classclazzparam:clazzparams) {sbuilder.append (Clazzparam.getname ()). Append (', ');                  } if (clazzparams.length==0) Sbuilder.deletecharat (Sbuilder.length ()-1);                  Sbuilder.append (') ');           System.out.println (Sbuilder); } SyStem.out.println ("-------------begincerate instance--------------");           Constructorconstructor = Clazzproxy1.getconstructor (Invocationhandler.class); ClassMyInvocationHandler1 implements invocationhandler{@Override Publicobject Invo Ke (Object arg0, Method arg1, object[] arg2) throwsthrowable {//tod                  O auto-generated method Stub returnnull;                     }           };           Collectionproxy1 = (Collection) constructor.newinstance (New MyInvocationHandler1 ()); System.out.println ("xxxxxxxxx" +proxy1.tostring ());//A method that has no return value can be called. A method that has a return value can be called//proxy1.size ();//cannot be called because it does not specify a target proxy1.clear ();//Can call COLLECTIONP Roxy2 = (Collection) constructor.newinstance (new Invocationhandler () {arraylisttarget = new ArrayList                  (); @Override Publicobject Invoke(Object proxy, method, object[] args) throwsthrowable {long                         BeginTime = System.currenttimemillis ();                         Objectretval = Method.invoke (Target,args);                         Longendtime = System.currenttimemillis ();                         System.out.println (Method.getname () + "Run Time" + (endtime-begintime));                  Returnretval;           }                            });           Proxy2.add ("Zxx");           Proxy2.add ("Java01");           Proxy2.add ("Java");                     Proxy2.size ();                         Collectionproxy3 = (Collection) proxy.newproxyinstance (Collection.class.getClassLoader (),                                Newclass[]{collection.class}, Newinvocationhandler () {                                Arraylisttarget = new ArrayList (); @Override Publicobject Invoke (Object proxy, MethOd method, object[] args) throwsthrowable {                                       Longbegintime = System.currenttimemillis ();                                       Objectretval = Method.invoke (Target,args);                                       Longendtime = System.currenttimemillis ();                                       System.out.println (Method.getname () + "Run Time:" + (Endtime-begintime));                                Returnretval;           }                         }                         );           Proxy3.add ("Java01");           Proxy3.add ("Java02");           Proxy3.add ("Java03");    System.out.println (Proxy3.size ());   }    }


Dark Horse programmer--java High-Tech (10) _ Dynamic agent and AOP programming

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.