我們知道Spring主要有兩大思想,一個是IoC,另一個就是AOP,對于IoC,依賴注入就不用多說了,而對于Spring的核心AOP來說,我們不但要知道怎麼通過AOP來滿足的我們的功能,我們更需要學習的是其底層是怎麼樣的一個原理,而AOP的原理就是java的動态代理機制,是以本篇随筆就是對java的動态機制進行一個回顧。
在java的動态代理機制中,有兩個重要的類或接口,一個是 InvocationHandler(Interface)、另一個則是 Proxy(Class),這一個類和接口是實作我們動态代理所必須用到的。首先我們先來看看java的API幫助文檔是怎麼樣對這兩個類進行描述的:
InvocationHandler:
InvocationHandler is the interface implemented by the invocation handler of a proxy instance.
Each proxy instance has an associated invocation handler. When a method is invoked on a proxy instance, the method invocation is encoded and dispatched to the invoke method of its invocation handler.
每一個動态代理類都必須要實作InvocationHandler這個接口,并且每個代理類的執行個體都關聯到了一個handler,當我們通過代理對象調用一個方法的時候,這個方法的調用就會被轉發為由InvocationHandler這個接口的 invoke 方法來進行調用。我們來看看InvocationHandler這個接口的唯一一個方法 invoke 方法:
Object invoke(Object proxy, Method method, Object[] args) throws Throwable
我們看到這個方法一共接受三個參數,那麼這三個參數分别代表什麼呢?
Object invoke(Object proxy, Method method, Object[] args) throws Throwable
proxy: 指代我們所代理的那個真實對象
method: 指代的是我們所要調用真實對象的某個方法的Method對象
args: 指代的是調用真實對象某個方法時接受的參數
如果不是很明白,等下通過一個執行個體會對這幾個參數進行更深的講解。
接下來我們來看看Proxy這個類:
Proxy provides static methods for creating dynamic proxy classes and instances, and it is also the superclass of all dynamic proxy classes created by those methods.
Proxy這個類的作用就是用來動态建立一個代理對象的類,它提供了許多的方法,但是我們用的最多的就是 newProxyInstance 這個方法:
public static Object newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h) throws IllegalArgumentException
Returns an instance of a proxy class for the specified interfaces that dispatches method invocations to the specified invocation handler.
這個方法的作用就是得到一個動态的代理對象,其接收三個參數,我們來看看這三個參數所代表的含義:

public static Object newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h) throws IllegalArgumentException
loader: 一個ClassLoader對象,定義了由哪個ClassLoader對象來對生成的代理對象進行加載
interfaces: 一個Interface對象的數組,表示的是我将要給我需要代理的對象提供一組什麼接口,如果我提供了一組接口給它,那麼這個代理對象就宣稱實作了該接口(多态),這樣我就能調用這組接口中的方法了
h: 一個InvocationHandler對象,表示的是當我這個動态代理對象在調用方法的時候,會關聯到哪一個InvocationHandler對象上

好了,下面給出代理樣例:
首先我們定義了一個HjzggMethod類型的接口
package com.hjzgg.proxy;
public interface HjzggMethod {
public int addMethod(int a, int b);
public int subMethod(int a, int b);
}
接着,定義了一個類來實作這個接口,這個類就是我們的真實對象,HjzggMethodImpl 類
package com.hjzgg.proxy;
public class HjzggMethodImpl implements HjzggMethod {
@Override
public int addMethod(int a, int b) {
System.out.println("我執行了!");
return a+b;
}
@Override
public int subMethod(int a, int b) {
return a-b;
}
}
建立動态代理類,這個類并沒有實作InvocationHandler ,而是在類方法中間接的建立一個InvocationHandler 執行個體
package com.hjzgg.proxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.util.Arrays;
public class MethodProxy {
private HjzggMethod target;
public MethodProxy(HjzggMethod target)
{
super();
this.target = target;
}
public HjzggMethod getMethodProxy()
{
HjzggMethod proxy = null;
//代理對象由哪一個類加載器加載
ClassLoader loader = target.getClass().getClassLoader();
//代理對象的類型,即其中有哪些方法
//Class[] interfaces = new Class[]{HjzggMethod.class};
Class[] interfaces = target.getClass().getInterfaces();
//當調用代理對象其中的方法時,該執行的代碼
InvocationHandler h = new InvocationHandler(){
@Override
public Object invoke(Object proxy, java.lang.reflect.Method method, Object[] args) throws Throwable {
String methodName = method.getName();
System.out.println(method);
System.out.println("the method: " + methodName + "開始, 參數: "+Arrays.asList(args));
Object result = method.invoke(target, args);
System.out.println("the method: "+methodName+"結束, 結果: " + result);
return result;
}
};
proxy=(HjzggMethod) Proxy.newProxyInstance(loader, interfaces, h);
return proxy;
}
}
最後測試類如下:
package com.hjzgg.proxy;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
public class ProxyTest {
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("}");
}
public static void main(String[] args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
HjzggMethod method = new HjzggMethodImpl();
System.out.println(method.getClass().getMethod("addMethod", int.class, int.class));
HjzggMethod methodProxy = new MethodProxy(method).getMethodProxy();
methodProxy.addMethod(10, 20);
printClassDefinition(methodProxy.getClass());
}
}
最後的輸出結果:
public int com.hjzgg.proxy.HjzggMethodImpl.addMethod(int,int)
public abstract int com.hjzgg.proxy.HjzggMethod.addMethod(int,int)
the method: addMethod開始, 參數: [10, 20]
the method: addMethod結束, 結果: 30
$Proxy0 extends java.lang.reflect.Proxy implements com.hjzgg.proxy.HjzggMethod
{
java.lang.reflect.Method m1;
java.lang.reflect.Method m3;
java.lang.reflect.Method m0;
java.lang.reflect.Method m4;
java.lang.reflect.Method m2;
int subMethod(int, int);
boolean equals(java.lang.Object);
java.lang.String toString();
int hashCode();
int addMethod(int, int);
}
從輸出結果來看,原來通過 HjzggMethod methodProxy = new MethodProxy(method).getMethodProxy();得到的HjzggMethod 執行個體其實是繼承自Proxy 的,并且實作了HjzggMethod (我們之前定義的接口)接口中的方法。在實作的方法(比如addMethod)中是通過 InvocationHandler 調用invoke方法,然後InvocationHandler的invoke方法中又調用method中的invoke來實作。執行的順序是 methodProxy.addMethod(10, 20); -> InvocationHandler中的invoke() -> method中的invoke()。還有就是method這個對象是可以通過接口.class來獲得的。method.invoke(obj, args)中的obj實體一定是實作了該method對應的接口。恰巧我們在建立代理對象的時候,(HjzggMethod) Proxy.newProxyInstance(loader, interfaces, h),也有interfaces(接口的位元組碼檔案對象)。
通過下面的例子,你就可以輕松的了解InvocationHandler中invoke()方法中的method是如何得來的!
Method method = HjzggMethod.class.getMethod("addMethod", int.class, int.class);//通過HjzggMethod接口得到Methed執行個體
HjzggMethod hjzggMethod = new HjzggMethodImpl();//建立實作HjzggMethod接口的HjzggMethodImpl對象
method.invoke(hjzggMethod, 10, 20);//執行方法, 相當于hjzggMethod.addMethod(10, 20);