The concept of proxy mode: Provides a proxy for other objects to control access to this object.
Application scenario: In the development of some business class methods, in addition to the requirements of operations, but also need to do other non-business operations. such as the interface provided to the app. In addition to the business logic to be implemented by the interface, the information of the user, the information of the device, and the encryption and decryption of the parameters are also verified. This is called before each interface method, and the business code is duplicated and cumbersome when it is joined together. These frequently used methods can be extracted from the proxy class. The business class only takes care of business operations and reduces coupling.
Static proxy
The agent and the Proxied object are determined before the agent. They all implement the same interface or inherit the same abstract class.
Dynamic Agent
Dynamically generate proxies for different classes, different methods of proxy
1) JDK Dynamic agent
Java provides dynamic proxy technology that allows developers to create proxy instances of the interface at run time. The JDK dynamic agent mainly involves the proxy and Invocationhandler under the Java.lang.reflect package.
Interface
Public Interface traffictools{ publicvoid Drive ();
Realize
Public class Implements traffictools { @Override publicvoid Drive () { System.out.println ("car driving ... "); }}
proxy class
ImportJava.lang.reflect.InvocationHandler;ImportJava.lang.reflect.Method; Public classCarproxyImplementsinvocationhandler{ PublicCarproxy (Object target) {Super(); This. target =Target; } PrivateObject Target; @Override Publicobject Invoke (Object proxy, Method method, object[] args)throwsThrowable {LongStart =System.currenttimemillis (); System.out.println ("The car began to drive ... "); Method.invoke (target); LongEnd =System.currenttimemillis (); System.out.println ("The car ended up driving ... "+ (End-start)); return NULL; }}
Test agent
Import Java.lang.reflect.Proxy; Public class Test { publicstaticvoid main (string[] args) { new Car (); New carproxy (car); Class<?> cls = car.getclass (); = (Traffictools) proxy.newproxyinstance (Cls.getclassloader (), cls.getinterfaces (), h); T.drive (); }}
2) cglib Dynamic Agent
The JDK dynamic agent can only create dynamic proxies for interfaces, and Cglib uses very low-level bytecode technology to create proxies for a class. A jar package that references cglib is required.
ImportJava.lang.reflect.Method;ImportNet.sf.cglib.proxy.Enhancer;ImportNet.sf.cglib.proxy.MethodInterceptor;ImportNet.sf.cglib.proxy.MethodProxy; Public classCglibproxyImplementsmethodinterceptor{PrivateEnhancer enhancer =Newenhancer (); PublicObject getproxy (Class clazz) {enhancer.setsuperclass (clazz); Enhancer.setcallback ( This); returnenhancer.create (); } /*** * Intercept method calls for all target classes*/@Override PublicObject Intercept (Object obj, Method m, object[] args, methodproxy proxy)throwsThrowable {returnproxy.invokesuper (obj, args); }}
The performance of dynamic proxy objects created by Cglib is much higher than the performance of the proxy objects created by the JDK. However, Cglib takes more time to create a proxy object than the JDK dynamic agent.
Java Proxy Mode