Dark Horse Programmer -- Java high-tech (10) _ dynamic proxy and AOP Programming

Source: Internet
Author: User

Dark Horse Programmer -- Java high-tech (10) _ dynamic proxy and AOP Programming

------ Java training, Android training, iOS training, and. Net training. We look forward to communicating with you! -------

I. Dynamic Core

The two cores of dynamic Proxy: Proxy and InvocationHandler are all interface classes under the Java. lang. reflect package. It can also be seen that dynamic Proxy is a reflection advanced application!

How is proxy and reflection reflected? The construction method and parameters of the agent creation process can be seen as follows:

1. The core parameter Method of invoke (), the unique method of the Handler at the core of the proxy, is a Class type. When the proxy is called, invoke returns retval to the target object.

2. the constructor parameters of the proxy, that is, the first two parameters of the newProxyInstance () method of Niu X, are the ClassLoader and Interface [] of the target Interface, which are also reflected to get through the Interface Class object;

2. Proxy call Method

Object invoke (Object proxy, Method method, Object [] args) Method. This method is called on the proxy instance and returns results.

Each proxy instance has an associated call handler. When a proxy instance calls a method, it will encode the method call and assign it to its call handler's invoke method.

The three parameters mean:

Proxy instance on which the Object proxy calls the Method

The Method method corresponds to the Method Instance of the interface Method called on the proxy instance.

Object [] args contains an array of objects that pass in the parameter values of method calls on the proxy instance. If the interface method does not use parameters, the value is null.

3. Create a proxy instance

Method 1. 1. Use the Proxy. getProxyClass () method to create Class files for several interfaces,

2. The Class file can be used to obtain the constructor.

3. Finally, obtain the proxy instance through the newInstance method of the constructor instance.

Method 2: Use the newProxyInstance () method of Proxy to directly return a Proxy instance with a specified call handler containing the Proxy class,

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

The parameter list of this method is:

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

Class [] Interfaces, which provides the interface for creating a proxy instance

InvocationHandlerh, which provides the interface implemented by the call handler of the proxy instance

Obviously, the second method is to re-encapsulate the code of the proxy logic, which is conducive to the compilation and management of the three agent building parameters, so that you can focus on processing the invoke () method, target object, aspect information (Advice) to implement AOP programming;

The establishment of the Proxy instance and the application of further AOP programming are as follows:

Static void clazzProxy () throws Exception {ClassclazzProxy1 = Proxy. getProxyClass (Collection. class. getClassLoader (), Collection. class); System. out. println (clazzProxy1.getName (); System. out. println ("--------------- constructorslist ------------"); Constructor [] constructors = clazzProxy1.getConstructors (); for (Constructorconstructor: constructors) {Stringname = constructor. getName (); StringBuildersB Uilder = 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 --------------"); Constru Ctorconstructor = clazzProxy1.getConstructor (InvocationHandler. class); implements InvocationHandler {@ Override publicObject invoke (Object arg0, Method arg1, Object [] arg2) throwsThrowable {// TODO Auto-generated method stub returnnull ;}}; collectionproxy1 = (Collection) constructor. newInstance (new MyInvocationHandler1 (); System. out. println ("xxxxxxxxx" + proxy1.toString ()); // A method without a return value can be called. A method with a returned value can be called // proxy1.size (); // it cannot be called because it does not specify the target proxy1.clear (); // you can call Collectionproxy2 = (Collection) constructor. newInstance (new InvocationHandler () {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 () + "running 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 ());}}


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.