1, Proxy is a design mode, provides additional access to the target object, that is, access to the target object through the proxy object. The advantage of this is that it can be enhanced by the implementation of the target object, that is, the ability to extend the target object. Here is an idea used in programming: Do not feel free to modify the code or methods that others have written, if you need to change, you can extend the method by proxy way
2, Java Three kinds of proxy mode:
2.1) Static proxy : When used, you need to define the interface or the parent class, the proxy object with the proxy object to implement the same interface or inherit the same parent class, and then invoke the same method to invoke the target object's method.
Advantages: Can be done without modifying the function of the target object under the premise of the target function extension.
Disadvantage: Because the proxy object needs to implement the same interface as the target object, there are many proxy classes and too many classes. At the same time, once the interface is incremented, the target object and the proxy object are maintained
Example
The same interface to be implemented by the proxy object and the proxy object
Public Interface Iuserdao { void Save ();}
View Code
Target Object
Public class Implements iuserdao{ publicvoid Save () { System.out.println ("----Saved data!----" ); }}
View Code
Proxy Object
Public classUserdaoproxyImplementsiuserdao{//receive Save target object PrivateIuserdao Target; PublicUserdaoproxy (Iuserdao target) { This. target=Target; } Public voidSave () {System.out.println ("Start transaction ..."); Target.save (); //methods for executing target objectsSYSTEM.OUT.PRINTLN ("Commit Transaction ..."); }}View Code
Test class
Public class TestApp { publicstaticvoid main (string[] args) { // target object new Userdao (); // Proxy object, the target object is passed to the proxy object, the agent relationship is established. New Userdaoproxy (target); Proxyobj.save (); }}
View Code
2.2) Dynamic Agent
A, dynamic proxy objects do not need to implement interfaces, but you need to specify the interface type. (The target object must implement the interface, otherwise it cannot use dynamic proxy);
B, proxy object generation, is the use of JDK API, dynamic in-Memory build proxy object (we need to specify the type of interface to create proxy object/target object implementation);
C, dynamic agent is also called: JDK agent, interface agent;
Agent Factory
ImportJava.lang.reflect.InvocationHandler;ImportJava.lang.reflect.Method;ImportJava.lang.reflect.Proxy; Public classProxyfactory {//maintain a target object PrivateObject Target; Publicproxyfactory (Object target) { This. target =Target; } //to generate a proxy object for a target object PublicObject getproxyinstance () {//Target.getclass (). getClassLoader () specifies that the current target object uses the class loader//Target.getclass (). Getinterfaces () The type of interface implemented by the target object//The new Invocationhandler () event handler, which executes the method of the target object, triggers the event handler, passing the method of the current execution target object as a parameter returnproxy.newproxyinstance (Target.getclass (). getClassLoader (), Target.getclass (). Getinterfaces (),NewInvocationhandler () { Publicobject Invoke (Object proxy, Method method, object[] args)throwsthrowable {System.out.println ("Start Transaction 2"); //execute target object methodObject returnvalue =Method.invoke (target, args); System.out.println ("Commit TRANSACTION 2"); returnreturnvalue; } }); }}View Code
The test class (the code that needs to use the proxy) first establishes the contact between the target object and the proxy object, and then proxies the same name method in the proxy object
ImportDaiLi.jingTaiDaiLi.IUserDao;ImportDaiLi.jingTaiDaiLi.UserDao; Public classTestApp { Public Static voidMain (string[] args) {//target ObjectIuserdao target =NewUserdao (); //"The original type class cn.itcast.b_dynamic. Userdao "System.out.println (Target.getclass ()); //to the target object, create a proxy objectIuserdao proxy = (Iuserdao)Newproxyfactory (target). Getproxyinstance (); //class $Proxy 0 dynamically generated proxy objects in memorySystem.out.println (Proxy.getclass ()); //execution Method "proxy object"Proxy.save (); }}View Code
2.3) Cglib Agent
A, Cglib proxy above the static agent and dynamic proxy mode is to require the target object is to implement an interface target object, but sometimes the target object is just a separate object, and do not implement any interface, this time can use the object subclass of the way class implementation proxy, this method is called : Cglib agent B, Cglib Proxy, also known as sub-class proxy, it is in memory to build a subclass object to achieve the function of the target object extension Cglib is a powerful high-performance code generation package, It can extend Java classes and implement Java interfaces at run time. It is widely used by many AOP frameworks, such as spring AOP and SYNAOP, to provide them with methods of interception (interception) The bottom of the Cglib package is to convert bytecode and generate new classes by using a small and fast bytecode processing framework ASM. Direct use of ASM is discouraged because it requires you to be familiar with the format and instruction set of the JVM's internal structure including the class file. Note: The dynamic agent of the JDK has a limitation that an object using a dynamic proxy must implement one or more interfaces, and if you want to broker a class that does not implement an interface, you can use the Cglib implementation. C, Cglib sub-class proxy implementation method :1 need to introduce the Cglib jar file, but spring's core package has included the Cglib function, so directly into the spring-core-3.2.5. Jar . 2) After introducing the feature pack, you can dynamically build the subclass 3 in memory ) The proxy class cannot be final, otherwise the error 4) The method of the target object is final/static, Then it will not be intercepted, i.e. the target object's additional business methods will not be executed.
View Code
Cglib Agent
ImportJava.lang.reflect.Method;ImportOrg.springframework.cglib.proxy.Enhancer;ImportOrg.springframework.cglib.proxy.MethodInterceptor;ImportOrg.springframework.cglib.proxy.MethodProxy; Public classProxyfactoryImplementsmethodinterceptor{//Maintaining target objects PrivateObject Target; Publicproxyfactory (Object target) { This. target =Target; } //Create a proxy object for the target object PublicObject getproxyinstance () {//1. Tool ClassEnhancer en =Newenhancer (); //2. Set the parent classEn.setsuperclass (Target.getclass ()); //3. Setting the callback functionEn.setcallback ( This); //4. Creating subclasses (proxy objects) returnen.create (); } PublicObject Intercept (Object obj, Method method, object[] args, Methodproxy proxy)throwsthrowable {System.out.println ("Start transaction ..."); //methods for executing target objectsObject returnvalue =Method.invoke (target, args); System.out.println ("Commit Transaction ..."); returnreturnvalue; }}View Code
Test class
ImportDaiLi.jingTaiDaiLi.UserDao; Public classTestApp { Public Static voidMain (String args[]) {//target ObjectUserdao target =NewUserdao (); //Proxy ObjectUserdao proxy = (Userdao)Newproxyfactory (target). Getproxyinstance (); //methods for executing proxy objectsProxy.save (); }}View Code
Iv. Proxy Mode