mybatis插件
1. Interceptor 就是個接口,供其它插件實現這個接口有三個方法
Object intercept(Invocation invocation)throws Throwable;
傳入一個Invocation 對象,我猜應該是指定攔截具體某個target的某個方法
這裏面應該寫具體的攔截策略,befor after。調用invocation.proceed()執行具體的方法
Object plugin(Object target);
將目標對象與插件綁定
void setProperties(Properties properties);
設置屬性
2. Invocation 是一個對象類
對象內部包括目標對象、待執行的方法、參數列表
public Object proceed() 包裝了一下method.invoke(target,args)
3. Plugin這個類比較重要
這個類有兩個比較重要的方法,一個
public staticObject wrap(Object target, Interceptor interceptor)
這是個靜態方法,作為工具用
將target和interceptor兩個對象包裝在一起。返回代理類對象
實現了Interceptor接口的對象中plugin方法可以調用這個靜態方法來實現將intercepter和target綁定到一起
這個方法調用了getSignatureMap方法來取寫了antonation的方法,可能是通過註解來標註哪些類需要攔截吧
另一個
publicObject invoke(Object proxy, Method method, Object[] args)
這個方法實現了InvocationHandler接口必須要實現這個方法,並且這個方法在調用對象的方法時是一定要執行的。
這個方法裏也比較簡單,就是判斷有沒有攔截器,有執行攔截器,否則直接執行方法
Set<Method> methods = signatureMap.get(method.getDeclaringClass());
if(methods != null&& methods.contains(method)) {
return interceptor.intercept(new Invocation(target, method, args));
}
return method.invoke(target, args);
4. 有個問題就是這幾個插件類具體怎麽用
1) 首先一定是要實現Interceptor,這個接口裏應該有個成員變量來存target吧?通過plugin方法綁定。Plugin方法裏應該是調用了Plugin. Wrap這個靜態方法。將綁定後的類返回
2) 用戶使用時直接new 一個Plugin對象,將target、interceptor、signatureMap傳進得去。這樣再調用mybatis插件