First, the origin of dynamic agents
Agent refers to: to each specific class to write a proxy class, in the future to use a specific class, as long as the object to create its proxy class, and then invoke the proxy class method can be, but if there are many specific classes, it is necessary to create a lot of proxy class can, this is obviously not suitable, the dynamic agent was born.
Second, there are 2 ways to implement dynamic Agent in Java:
1, the JDK implementation of dynamic Proxy, but it needs to implement the class through the interface to define the business method, for the class without an interface is not possible;
2, CGLib, it uses a very low-level bytecode technology, the principle is to use bytecode technology for a class to create subclasses, and in the subclass of the method interception techniques to intercept all the parent class method calls.
Both the JDK dynamic agent and the Cglib Dynamic agent are the basis for implementing Spring AOP.
Third, the implementation of the JDK
This type of dynamic proxy uses an interface Invocationhandler and a proxy class, these two classes can be used together to implement dynamic proxy.
Interface Interface interfaceclass { void show () ;} Concrete Realization class aclass classa implements interfaceclass { @Override public void show () { System.out.println ("Class a") ; }}//Specific implementation class Bclass classb implements InterfaceClass { @Override public void show () { system.out.println ("class b") ; }}//dynamic proxy class to implement Invocationhandler interface class invoker implements invocationhandler { InterfaceClass ia ; public Invoker (Interfaceclass ia) { this.ia = ia ; } @Override public object invoke (object proxy, METHOD METHOD, OBJECT[] ARG) throws Throwable { method.invoke (Ia, arg) ; return null ; }}// Testclass DynamicProxyTest{ Public static void main (String[] args) { // Create a ClassA object for a specific class invoker invoker1 = new invoker (New classa ()); // get agents for specific classes of ClassA InterfaceClass ic1 = (Interfaceclass) Proxy.newproxyinstance (InterfaceClass.class.getClassLoader (), new class[]{interfaceclass.class}, invoker1); &Nbsp; ic1.show () ;     // CLASSB is so }}
Iv. realization of Cglib
public class cglibproxy implements methodinterceptor{ private Enhancer enhancer = new enhancer () ; Public object getproxy (Class clazz) { // Set Parent class enhancer.setsuperclass (Clazz); enhancer.setcallback (this); // Dynamically create sub-class strength by bytecode technology return enhancer.create () ; } // all methods will be intercepted by this method, This class implements methods and proxies for subclasses. The GetProxy (Superclass.class) method creates a proxy object by extending the class class of the parent class by entering the byte code of the parent class. The Intercept () method intercepts the invocation of all target class methods, and obj represents an instance of the target class, and args is the dynamic entry parameter of the method, and proxy is the instance of the agent class. @Override public object intercept (Object obj, method&Nbsp;method, object[] args, methodproxy methodproxy) throws Throwable{ object result = methodproxy.invokersuper (obj, args) ; return result ; } }public class userserviceimpl{ public void sayhello () { system.out.println ("Sayhello method") ; } public void saybye () { system.out.println ("Saybye method") ; }}// testpublic class test{ public static void main (String[] args) { CGLIBProxy proxy = new Cglibproxy () ; UserServiceImpl impl = ( Userserviceimpl) Proxy.getproxy (userserviceimpl.class) ; Impl.sayhello () ; }}
Vi. comparative advantages and disadvantages of the two approaches
The dynamic proxy object created by Cglib has a much higher performance than the dynamic proxy object created by the JDK, but Cglib spends a lot more time creating proxy objects than the JDK, so for singleton objects, it is better to use the cglib instead of creating objects frequently, rather than using the JDK in a more appropriate way. At the same time, the method used to create subclasses for the dynamic proxy used by Cglib cannot be processed for the final method.
Java Dynamic Agent