Comparison and performance analysis of CGLIB agent and static proxy

The CGLIB agent and static proxy are two common proxy mode.This article will be compared and analyzed from three aspects: principle, use scenario and performance. 1. Principle comparison Static proxy creates a proxy class during the compilation period, the proxy class and the proxy classes implement the same interface, and at the same time hold a reference to the agent class in the agency class, to realize the proxy by calling the proxy class.The CGLIB proxy is to achieve proxy by inheriting the agent class and generating a new subclasses.Subclass rewrite the method of being proxy, so as to intercept and enhance the method. 2. Use scene comparison Static proxy is suitable for the same interface as the proxy object as only one or a small number of interfaces, and the proxy objects are the same interface as the proxy object. It is usually used to add additional logic in some specific scenarios, such as log records and authority control.The CGLIB proxy is suitable for no interface and needs to be proxy to the class. CGLIB can achieve proxy by inheriting the proxy class, so it can intercept the non -final method of the agent class. 3. Performance analysis In terms of performance, the performance of static proxy is high because it generates an agent class during the compilation period to avoid the cost of dynamic generating categories during runtime.The CGLIB agent uses bytecode generating technology when runtime, and a new proxy class needs to be generated through the byte code framework of ASM. Therefore, it will have a certain performance loss relative to the static agent. Below are examples of Java code of two proxy modes: 1. Static proxy example: // Definition interface public interface Subject { void request(); } // public class RealSubject implements Subject { public void request() { System.out.println ("Realsubject processing request"); } } // proxy public class ProxySubject implements Subject { private RealSubject realSubject; public ProxySubject(RealSubject realSubject) { this.realSubject = realSubject; } public void request() { System.out.println ("Proxysubject processing request"); Realsubject.request (); // Call the method of the proxy class System.out.println ("Proxysubject processing request"); } } // Test code public class Main { public static void main(String[] args) { RealSubject realSubject = new RealSubject(); ProxySubject proxySubject = new ProxySubject(realSubject); proxySubject.request(); } } 2. CGLIB proxy example: // public class RealSubject { public void request() { System.out.println ("Realsubject processing request"); } } // Method interceptor public class MyMethodInterceptor implements MethodInterceptor { public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { System.out.println ("Before CGLIB proxy"); Object result = proxy.invokesuper (obj, args); // Call the method of the proxy System.out.println ("CGLIB Agent"); return result; } } // Test code public class Main { public static void main(String[] args) { Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(RealSubject.class); enhancer.setCallback(new MyMethodInterceptor()); RealSubject proxySubject = (RealSubject) enhancer.create(); proxySubject.request(); } } Summarize: Through the above comparison and analysis, we can see that the static proxy is suitable for the interface agent with a limited method, and the performance is high; while the CGLIB proxy is suitable for the situation without interfaces or the need to proxy the class, and the performance is low.In practical applications, the appropriate proxy mode is selected according to specific needs to achieve dynamic proxy.