Java Dynamic agent and Cglib

Source: Internet
Author: User

1. Proxy classes can be divided into two types.
Static proxy: The source code is generated automatically by the programmer or a specific tool, and then compiled. Before the program runs, the. class file for the proxy classes already exists.
Dynamic Agent: When the program is running, it is created dynamically using the reflection mechanism.

2.JDK dynamic proxies rely on the interface implementation, if some classes do not implement the interface, you can not use the JDK proxy, this will use Cglib dynamic proxy.

3.Cglib Dynamic Agent
The dynamic agent mechanism of JDK can only implement the class of the interface, and the class that cannot implement the interface cannot implement the dynamic proxy of the JDK, the cglib is to implement the proxy for the class, his principle is to generate a subclass for the target class, and overwrite the method implementation enhancement, but because inherit is adopted, Therefore, the final decorated class cannot be proxied.

4.

The JDK dynamic agent contains a class and an interface:
Invocationhandler Interface:
Public interface Invocationhandler {
public object Invoke (Object Proxy,method method,object[] args) throws Throwable;
}
Parameter description:
Object proxy: Refers to the objects being proxied.
Method: Methods to invoke
object[] args: Parameters required for method invocation

The subclass of the Invocationhandler interface can be imagined as a proxy's final operation class, replacing the Proxysubject.

Proxy class:
The proxy class is an action class that specifically completes the proxy, which can be used to dynamically generate an implementation class for one or more interfaces, which provides the following operations:
public static Object newproxyinstance (ClassLoader loader, class<?>[] interfaces,
Invocationhandler h)
Throws IllegalArgumentException
Parameter description:
ClassLoader Loader: Class loader
Class<?>[] Interfaces: Get all the interfaces
Invocationhandler h: Get subclass instance of Invocationhandler interface

Ps: Class Loader
In the proxy class in the Newproxyinstance () method requires an instance of the ClassLoader class, ClassLoader actually corresponds to the class loader, in Java, there are three kinds of loaders;
Booststrap ClassLoader: This loader is written in C + + and is not visible in general development;
Extendsion ClassLoader: Used to load the extension class, generally corresponding to the class in the Jre\lib\ext directory;
Appclassloader: (default) Loads the class specified by Classpath, which is most commonly used as an loader.

Dynamic Agent
Compared with the static proxy class, dynamic proxy class, the bytecode of dynamic proxy class is generated dynamically by the Java reflection mechanism when the program runs, without the programmer writing its source code manually. The dynamic proxy class not only simplifies programming, but also improves the scalability of the software system, because the Java reflection mechanism can generate any type of dynamic proxy class. The proxy class and the Invocationhandler interface in the Java.lang.reflect package provide the ability to generate dynamic proxy classes.

For example:

 Public Interface Oderfacade {      publicvoid  addorder ();  
 Public class Implements Orderfacade {        @Override      publicvoid  AddOrder () {          System.out.println ( "Add Order");      }    }  
/*** JDK Dynamic Proxy proxy class **/   Public classOrderfacadeproxyImplementsInvocationhandler {PrivateObject Target; /*** Bind the delegate object and return a proxy class *@paramTarget *@return      */       Publicobject bind (object target) { This. target =Target; //Get proxy Object        returnproxy.newproxyinstance (Target.getclass (). getClassLoader (), Target.getclass (). Getinterfaces (), This);//to bind an interface (this is a flaw, cglib compensates for this flaw)} @Override/*** Call Method*/       Publicobject Invoke (Object proxy, Method method, object[] args)throwsthrowable {Object result=NULL; System.out.println ("Things start."); //Execution Methodresult=Method.invoke (target, args); System.out.println ("The end of things."); returnresult; }    }  
 Public class Testproxy {        publicstaticvoid  main (string[] args) {          new  orderfacadeproxy ();           = (Orderfacade) proxy.bind (new  Orderfacadeimpl ());           Orderproxy.addorder ();      }    

Java Dynamic agent and Cglib

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.