動態代理其實就是java.lang.reflect.Proxy類別動態的根據您指定的所有介面產生一個class byte,該class會繼承Proxy類,並且實作所有你指定的介面(您在參數中傳入的介面陣列) ;然後再利用您指定的classloader將class byte加載進系統,最後產生這樣一個類別的對象,並初始化該對象的一些值,如invocationHandler,以即所有的接口對應的Method成員。 初始化之後將物件傳回給呼叫的客戶端。這樣客戶端拿到的就是一個實作你所有的介面的Proxy物件。請看實例分析:
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;
/**
* 業務代理類
* @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();
}
很明顯,Proxy.newProxyInstance方法會做以下幾件事:
1,根據傳入的第二個參數interfaces動態產生一個類,實作interfaces中的接口,該例中即BusinessProcessor接口的processBusiness方法。並且繼承了Proxy類,重寫了hashcode,toString,equals等三個方法。具體實作可參考ProxyGenerator.generateProxyClass(...); 此例中產生了$Proxy0類
2,透過傳入的第一個參數classloder將剛產生的類別載入到jvm中。即將$Proxy0類load
3,利用第三個參數,呼叫$Proxy0的$Proxy0(InvocationHandler)建構函式建立$Proxy0的對象,並且用interfaces參數遍歷其所有介面的方法,並產生Method對像初始化對象的幾個Method成員變數
4,將$Proxy0的執行個體傳回給客戶端。
現在好了。我們再看客戶端怎麼調就清楚了。
1,客戶端拿到的是$Proxy0的實例對象,由於$Proxy0繼承了BusinessProcessor,因此轉換為BusinessProcessor沒任何問題。
BusinessProcessor bp = (BusinessProcessor)Proxy.newProxyInstance(....);
2,bp.processBusiness();
實際上呼叫的是$Proxy0.processBusiness();那麼$Proxy0.processBusiness()的實作就是透過InvocationHandler去呼叫invoke方法啦!