Proxy design uses many design modes in Java Development. The so-called proxy Design refers to the operation of real themes by a proxy topic, and the actual theme performs specific business operations, the proxy subject is responsible for processing other related services.
Let's take a lookStatic proxy
1 PackageCom. Proxy. Inter;2 3 /**4 * Define the demo Interface5*/6 Public InterfaceDemo {7Public VoidSave ();8}
1 Package Com. Proxy. impl; 2 3 Import Com. Proxy. Inter. Demo; 4 5 /** 6 * Demoimpl implements the demo interface and overwrites the SAVE () method. 7 * Real theme: execute specific services 8 */ 9 Public Class DemoimplImplements Demo { 10 Public Void Save (){ 11 System. Out. println ("Call the SAVE () method" ); 12 } 13 }
1 Package Com. Proxy. impl; 2 3 Import Com. Proxy. Inter. Demo; 4 /** 5 * Demoimplproxy also implements the demo interface, overwrites the SAVE () method, and adds its own business. 6 * Proxy subject, responsible for processing other services 7 */ 8 Public Class Demoimplproxy Implements Demo { 9 Demo demoimpl = New Demoimpl (); 10 11 Public Void Save (){ 12 System. Out. println ("start logging" ); 13 Demoimpl. Save (); 14 System. Out. println ("start to end log" ); 15 } 16 }
1 Package Com. Proxy. impl; 2 3 Import Com. Proxy. Inter. Demo; 4 5 /** 6 * Start logging 7 * Call the SAVE () method 8 * Start to end the log 9 */ 10 Public Class Test { 11 Public Static Void Main (string [] ARGs ){ 12 Demo demoimplproxy = New Demoimplproxy (); 13 14 Demoimplproxy. Save (); 15 } 16 }
You will find that each proxy class can only serve one interface.ProgramMany proxy classes will inevitably be generated during development
So we will find a way to complete all the proxy functions through a proxy class, then we need to use dynamic proxy
To implement the dynamic proxy mechanism in Java, java. Lang. Reflect. invocationhandler interface and Java. Lang. Reflect. proxy class are required.
Java. Lang. Reflect. invocationhandlerThe interface is defined as follows:
Public InterfaceInvocationhandler {PublicObject invoke (Object proxy, method, object [] ARGs) throws throwable ;}
Object Proxy: the proxy object.
Method method: method to be called
Object [] ARGs: parameters required for method calling
Java. Lang. Reflect. ProxyThe class is defined as follows:
Public StaticObject newproxyinstance (classloader loader, class <?> [] Interfaces, invocationhandler H)ThrowsIllegalargumentexception
Classloader Loader: Class Loader
Class <?> Interfaces: Get all interfaces
Invocationhandler H: obtains the instance of the subclass of the invocationhandler interface.
Next let's take a lookDynamic proxySpecific implementation
1 PackageCom. Proxy. Inter;2 3 /**4 * Define the demofirst Interface5*/6 Public InterfaceDemofirst {7Public VoidSavefirst ();8}
1 Package Com. Proxy. impl; 2 3 Import Com. Proxy. Inter. demofirst; 4 5 /** 6 * Demofirstimpl implements the demofirst interface and overwrites the savefirst () method. 7 * Real theme, responsible for executing specific services 8 */ 9 Public Class Demofirstimpl Implements Demofirst { 10 11 @ Override 12 Public Void Savefirst (){ 13 System. Out. println ("Call savefirst () method" ); 14 } 15 16 }
1 Package Com. Proxy. impl; 2 3 Import Java. Lang. Reflect. invocationhandler; 4 Import Java. Lang. Reflect. method; 5 6 /** 7 * Invocationhandlerimple implements the invocationhandler interface and overwrites the invoke () method. 8 * The Business of the proxy subject is written in the invoke () method. 9 */ 10 Public Class Invocationhandlerimpl Implements Invocationhandler { 11 12 Private Object target; 13 14 Public Invocationhandlerimpl (object target ){ 15 This . Target = Target; 16 } 17 18 @ Override 19 Public Object invoke (Object proxy, method, object [] ARGs) 20 Throws Throwable { 21 System. Out. println ("target:" + Target. getclass (). getname ()); 22 System. Out. println ("Proxy:" + Proxy. getclass (). getname ()); 23 System. Out. println ("method:" + Method. getname ()); 24 System. Out. println ("ARGs:" + ARGs ); 25 System. Out. println ("start logging" ); 26 Object OBJ = Method. Invoke (target, argS ); 27 System. Out. println ("End record log" ); 28 /* 29 * System. Out. println ("OBJ:" + obj. getclass (). getname ()); 30 * In this example, the savexxx method does not return a value, so OBJ reports a null pointer exception. 31 */ 32 Return OBJ; 33 } 34 }
1 Package Com. Proxy. impl; 2 3 Import Java. Lang. Reflect. proxy; 4 5 Import Com. Proxy. Inter. demofirst; 6 /* Import com. Proxy. Inter. demosecond; */ 7 8 Public Class Test { 9 Public Static Void Main (string [] ARGs ){ 10 Demofirst first = New Demofirstimpl (); 11 /* Demosecond second = new demosecondimpl (); */ 12 13 // Get proxy object 14 Demofirst firstproxy = (Demofirst) proxy. newproxyinstance (first 15 . Getclass (). getclassloader (), first. getclass (). getinterfaces (), 16 New Invocationhandlerimpl (first )); 17 // Dynamic proxy call Method 18 Firstproxy. savefirst (); 19 20 /* Demosecond secondproxy = (demosecond) proxy. newproxyinstance (second 21 . Getclass (). getclassloader (), second. getclass (). getinterfaces (), 22 New invocationhandlerimpl (second )); 23 Secondproxy. savesecond (); */ 24 } 25 }
Execution result
If we need a dynamic proxy for the demosecond object, we only need to do that as described in the above comments.
Execution result
We will find that the proxy class proxy will dynamically allocate the proxy object, $ proxy0, $ proxy1...
Dynamic proxy applications are embodied in many places, such as AOP In the open-source framework spring (for Aspect-Oriented Programming )...