Dynamic proxy is actually java. lang. reflect. the Proxy class dynamically generates a class byte based on all the interfaces you specified. This class inherits the Proxy class and implements all the interfaces you specified (the interface array you passed in the parameters ); then, use the specified classloader to load the class byte into the system, generate such a class object, and initialize some values of the object, such as invocationHandler, it is the Method member corresponding to all interfaces. After initialization, the object is returned to the called client. In this way, the client obtains a Proxy object that implements all your interfaces. See instance analysis:
Copy codeThe Code is as follows: package com. fans. common. proxy;
Public interface BusinessProcessor {
Public void processBusiness ();
}
Copy codeThe Code is as follows: package com. fans. common. proxy;
/**
* Business processing
* @ Author fanshadoop
*
*/
Public class BusinessProcessorImpl implements BusinessProcessor {
@ Override
Public void processBusiness (){
System. out. println ("processing business .....");
}
}
Copy codeThe Code is as follows: package com. fans. common. proxy;
Import java. lang. reflect. InvocationHandler;
Import java. lang. reflect. Method;
/**
* Service Proxy
* @ Author fanshadoop
*
*/
Public class BusinessProcessorHandler implements InvocationHandler {
Private Object target = null;
BusinessProcessorHandler (Object target ){
This.tar get = target;
}
Public Object invoke (Object proxy, Method method, Object [] args)
Throws Throwable {
System. out
. Println ("You can do something here before process your business ");
Object result = method. invoke (target, args );
System. out
. Println ("You can do something here after process your business ");
Return result;
}
}
Copy codeThe Code is as follows: package com. fans. common. proxy;
Import java. lang. reflect. Field;
Import java. lang. reflect. Method;
Import java. lang. reflect. Modifier;
Import java. lang. reflect. Proxy;
Public class Test {
/**
* @ Param args
*/
Public static void main (String [] args ){
BusinessProcessorImpl bpimpl = new BusinessProcessorImpl ();
BusinessProcessorHandler handler = new BusinessProcessorHandler (bpimpl );
BusinessProcessor bp = (BusinessProcessor) Proxy. newProxyInstance (
Bpimpl. getClass (). getClassLoader (), bpimpl. getClass ()
. GetInterfaces (), handler );
Bp. processBusiness ();
System. out. println (bp. getClass (). getName ());
PrintClassDefinition (bp. getClass ());
}
Public static String getModifier (int modifier ){
String result = "";
Switch (modifier ){
Case Modifier. PRIVATE:
Result = "private ";
Case Modifier. PUBLIC:
Result = "public ";
Case Modifier. PROTECTED:
Result = "protected ";
Case Modifier. ABSTRACT:
Result = "abstract ";
Case Modifier. FINAL:
Result = "final ";
Case Modifier. NATIVE:
Result = "native ";
Case Modifier. STATIC:
Result = "static ";
Case Modifier. SYNCHRONIZED:
Result = "synchronized ";
Case Modifier. STRICT:
Result = "strict ";
Case Modifier. TRANSIENT:
Result = "transient ";
Case Modifier. VOLATILE:
Result = "volatile ";
Case Modifier. INTERFACE:
Result = "interface ";
}
Return result;
}
Public static void printClassDefinition (Class clz ){
String clzModifier = getModifier (clz. getModifiers ());
If (clzModifier! = Null &&! ClzModifier. equals ("")){
ClzModifier = clzModifier + "";
}
String superClz = clz. getSuperclass (). getName ();
If (superClz! = Null &&! SuperClz. equals ("")){
SuperClz = "extends" + superClz;
}
Class [] interfaces = clz. getInterfaces ();
String inters = "";
For (int I = 0; I <interfaces. length; I ++ ){
If (I = 0 ){
Inters + = "implements ";
}
Inters + = interfaces [I]. getName ();
}
System. out. println (clzModifier + clz. getName () + "" + superClz + ""
+ Inters );
System. out. println ("{");
Field [] fields = clz. getDeclaredFields ();
For (int I = 0; I <fields. length; I ++ ){
String modifier = getModifier (fields [I]. getModifiers ());
If (modifier! = Null &&! Modifier. equals ("")){
Modifier = modifier + "";
}
String fieldName = fields [I]. getName ();
String fieldType = fields [I]. getType (). getName ();
System. out. println ("" + modifier + fieldType + "" + fieldName
+ ";");
}
System. out. println ();
Method [] methods = clz. getDeclaredMethods ();
For (int I = 0; I <methods. length; I ++ ){
Method method = methods [I];
String modifier = getModifier (method. getModifiers ());
If (modifier! = Null &&! Modifier. equals ("")){
Modifier = modifier + "";
}
String methodName = method. getName ();
Class returnClz = method. getReturnType ();
String retrunType = returnClz. getName ();
Class [] clzs = method. getParameterTypes ();
String paraList = "(";
For (int j = 0; j <clzs. length; j ++ ){
ParaList + = clzs [j]. getName ();
If (j! = Clzs. length-1 ){
ParaList + = ",";
}
}
ParaList + = ")";
Clzs = method. getExceptionTypes ();
String exceptions = "";
For (int j = 0; j <clzs. length; j ++ ){
If (j = 0 ){
Exceptions + = "throws ";
}
Exceptions + = clzs [j]. getName ();
If (j! = Clzs. length-1 ){
Exceptions + = ",";
}
}
Exceptions + = ";";
String methodPrototype = modifier + retrunType + "" + methodName
+ ParaList + exceptions;
System. out. println ("" + methodPrototype );
}
System. out. println ("}");
}
}
Running result:
Copy codeThe Code is as follows: You can do something here before process your business
Processing business .....
You can do something here after process your business
$ Proxy0
$ Proxy0 extends java. lang. reflect. Proxy implements com. fans. common. proxy. BusinessProcessor
{
Java. lang. reflect. Method m1;
Java. lang. reflect. Method m3;
Java. lang. reflect. Method m0;
Java. lang. reflect. Method m2;
Boolean equals (java. lang. Object );
Java. lang. String toString ();
Int hashCode ();
Void processBusiness ();
}
Class BusinessProcessorHandler implements the InvocationHandler interface's invoke method, this class is the Proxy finally calls the Fixed Interface Method.
Obviously, the Proxy. newProxyInstance method will do the following:
1. A class is dynamically generated based on the second input parameter interfaces to implement the interface in interfaces. In this example, the processBusiness method of the BusinessProcessor interface is used. It inherits the Proxy class and overwrites three methods, including hashcode, toString, and equals. For specific implementation, see ProxyGenerator. generateProxyClass (...). In this example, the $ Proxy0 class is generated.
2. Load the newly generated class to jvm using the first input parameter classloder. About $ Proxy0 class load
3. Call the $ Proxy0 (InvocationHandler) constructor of $ Proxy0 to create the $ Proxy0 object, and use the interfaces parameter to traverse all its interface methods, and generate several Method member variables of the Method object initialization object.
4. Return the $ Proxy0 instance to the client.
Now. Let's see how the client is tuned.
1. The client obtains the $ Proxy0 instance object. Since $ Proxy0 inherits BusinessProcessor, it is no problem to convert it to BusinessProcessor.
BusinessProcessor bp = (BusinessProcessor) Proxy. newProxyInstance (....);
2, bp. processBusiness ();
Actually, $ Proxy0.processBusiness () is called. Then, the implementation of $ Proxy0.processBusiness () is to call the invoke method through InvocationHandler!