Dynamic proxy is actually the java.lang.reflect.Proxy class that dynamically generates a class byte based on all the interfaces you specify. This class will inherit the Proxy class and implement all the interfaces you specify (the interface array you pass in the parameter) ; Then use the classloader you specified to load the class byte into the system, and finally generate an object of such a class, and initialize some values of the object, such as invocationHandler, which are the Method members corresponding to all interfaces. After initialization, the object is returned to the calling client. In this way, what the client gets is a Proxy object that implements all your interfaces. Please see the example analysis:
public interface BusinessProcessor {
public void processBusiness();
}
@Override
public void processBusiness() {
System.out.println("processing business....");
}
}
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
/**
*Business agency category
* @author fanshadoop
*
*/
public class BusinessProcessorHandler implements InvocationHandler {
private Object target = null;
BusinessProcessorHandler(Object target) {
this.target = 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;
}
}
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("}");
}
}
boolean equals(java.lang.Object);
java.lang.String toString();
int hashCode();
void processBusiness();
}
Obviously, the Proxy.newProxyInstance method will do the following things:
1. Dynamically generate a class based on the passed in second parameter interfaces to implement the interface in interfaces. In this example, it is the processBusiness method of the BusinessProcessor interface. And it inherits the Proxy class and rewrites three methods such as hashcode, toString, and equals. For specific implementation, please refer to ProxyGenerator.generateProxyClass(...); In this example, the $Proxy0 class is generated
2. Load the newly generated class into the jvm through the first parameter passed in, classloder. About to load $Proxy0 class
3. Use the third parameter to call the $Proxy0 (InvocationHandler) constructor of $Proxy0 to create an object of $Proxy0, and use the interfaces parameter to traverse all its interface methods and generate a Method object to initialize several Method member variables of the object.
4. Return the instance of $Proxy0 to the client.
It's fine now. Let’s see how the client adjusts it and it will become clear.
1. The client gets the instance object of $Proxy0. Since $Proxy0 inherits BusinessProcessor, there is no problem in converting it to BusinessProcessor.
BusinessProcessor bp = (BusinessProcessor)Proxy.newProxyInstance(....);
2. bp.processBusiness();
What is actually called is $Proxy0.processBusiness(); then the implementation of $Proxy0.processBusiness() is to call the invoke method through InvocationHandler!