一、Plugin用法簡介
注:本小節內容來源於Mybatis官方文檔,《傳送門》
MyBatis允許你在已映射語句執行過程中的某一點進行攔截調用。默認情況下,MyBatis 允許使用插件來攔截的方法調用包括:
- Executor (update, query, flushStatements, commit, rollback, getTransaction, close, isClosed)
- ParameterHandler (getParameterObject, setParameters)
- ResultSetHandler (handleResultSets, handleOutputParameters)
- StatementHandler (prepare, parameterize, batch, update, query)
這些類中方法的細節可以通過查看每個方法的簽名來發現,或者直接查看 MyBatis 發行包中的源代碼。 如果你想做的不僅僅是監控方法的調用,那麼你最好相當瞭解要重寫的方法的行爲。 因爲如果在試圖修改或重寫已有方法的行爲的時候,你很可能在破壞 MyBatis 的核心模塊。 這些都是更低層的類和方法,所以使用插件的時候要特別當心。
通過 MyBatis 提供的強大機制,使用插件是非常簡單的,只需實現 Interceptor 接口,並指定想要攔截的方法簽名即可。
// ExamplePlugin.java
@Intercepts({@Signature(
type= Executor.class,
method = "update",
args = {MappedStatement.class,Object.class})})
public class ExamplePlugin implements Interceptor {
private Properties properties = new Properties();
public Object intercept(Invocation invocation) throws Throwable {
// implement pre processing if need
Object returnObject = invocation.proceed();
// implement post processing if need
return returnObject;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
}
<!-- mybatis-config.xml -->
<plugins>
<plugin interceptor="org.mybatis.example.ExamplePlugin">
<property name="someProperty" value="100"/>
</plugin>
</plugins>
上面的插件將會攔截在 Executor 實例中所有的 “update” 方法調用, 這裏的 Executor 是負責執行低層映射語句的內部對象。
覆蓋配置類: 除了用插件來修改 MyBatis 核心行爲之外,還可以通過完全覆蓋配置類來達到目的。只需繼承後覆蓋其中的每個方法,再把它傳遞到 SqlSessionFactoryBuilder.build(myConfig) 方法即可。再次重申,這可能會嚴重影響 MyBatis 的行爲,務請慎之又慎。
二、Mybatis中的攔截器實現
1、類結構
在Mybatis的plugin模塊中,採用了責任鏈設計模式和JDK動態代理。其中使用動態代理來實現攔截器的功能,用來實現增強業務對象的功能;責任鏈設計模式主要用來實現多攔截器順序執行的問題。其中動態代理相關知識可以參考《設計模式之代理模式》。
plugin模塊在Mybatis源碼中的包路徑:org.apache.ibatis.plugin。具體包結構如下圖所示:
其中,Intercepts和Signature是註解,通過在實現 Interceptor接口的類添加該註解,實現對指定的類中的特定方法進行攔截;Interceptor是攔截器的接口定義;InterceptorChain是攔截器鏈的定義;Invocation是對目標對象的封裝;Plugin類是代理對象,同時又起到了動態代理對象中的工廠類的作用。下面分別詳細介紹各個類:
2、@Intercepts、@Signature註解
@Intercepts註解配合@Signature註解,實現了對指定類中特定方法進行攔截。其中@Intercepts註解定義了一個@Signature註解的集合,即Signature[]數組;@Signature註解用來定義需要攔截的方法,其中type屬性指需要攔截的類,method屬性指需要攔截的方法名稱,args屬性指定被攔截方法的參數列表;其實,根據@Signature註解的三個屬性,我們就可以唯一確定一個需要攔截的方法了。
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Intercepts {
Signature[] value();
}
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({})
public @interface Signature {
Class<?> type();
String method();
Class<?>[] args();
}
3、Invocation類
Invocation類是對被代理對象的封裝,其中封裝了被代理對象實例、目標方法、目標方法的參數列表。同時提供了proceed()方法用來調用目標方法。代碼比較簡單,如下所示:
public class Invocation {
private final Object target;
private final Method method;
private final Object[] args;
public Invocation(Object target, Method method, Object[] args) {
this.target = target;
this.method = method;
this.args = args;
}
public Object getTarget() {
return target;
}
public Method getMethod() {
return method;
}
public Object[] getArgs() {
return args;
}
/**
* 執行目標對象方法
* @return
* @throws InvocationTargetException
* @throws IllegalAccessException
*/
public Object proceed() throws InvocationTargetException, IllegalAccessException {
return method.invoke(target, args);
}
}
4、Interceptor 接口
Interceptor接口是Mybatis的plugin模塊的核心,所有用戶自定義的攔截器,都需要實現該接口。其中,可以被攔截的類在Plugin用法簡介中已經描述了。該接口代碼如下:
public interface Interceptor {
/**
* 實現攔截邏輯,內部要通過invocation.proceed()顯式地推進責任鏈前進,也就是調用下一個攔截器攔截目標方法。
*
* @param invocation
* @return
* @throws Throwable
*/
Object intercept(Invocation invocation) throws Throwable;
/**
* 用當前這個攔截器生成對目標target的代理,實際是通過Plugin.wrap(target,this)
* 來完成的,把目標target和攔截器this傳給了包裝函數。
*
* @param target
* @return
*/
Object plugin(Object target);
/**
* 設置額外的參數,參數配置在攔截器的Properties節點裏。
* @param properties
*/
void setProperties(Properties properties);
}
5、InterceptorChain 類
InterceptorChain 類用來記錄用戶定義的所有攔截器,底層其實就是用List集合進行了存儲。並提供了兩個通用方法,1、把攔截器添加到攔截器集合中的方法,即向List集合添加元素,2、爲被代理對象添加攔截器的方法,即通過interceptor.plugin()方法實現。具體代碼如下:
public class InterceptorChain {
/**
* 攔截器存儲變量
*/
private final List<Interceptor> interceptors = new ArrayList<Interceptor>();
/**
* 爲目標對象添加攔截器
*
* @param target
* @return
*/
public Object pluginAll(Object target) {
for (Interceptor interceptor : interceptors) {
target = interceptor.plugin(target);
}
return target;
}
/**
* 添加攔截器
* @param interceptor
*/
public void addInterceptor(Interceptor interceptor) {
interceptors.add(interceptor);
}
public List<Interceptor> getInterceptors() {
return Collections.unmodifiableList(interceptors);
}
}
6、Plugin類
Plugin類是Mybatis提供的一個工具類,同時實現了InvocationHandler接口。該類的作用有:
- 提供創建動態代理對象的方法,即wrap()方法。
- 實現了InvocationHandler接口,即本身就可以作爲一個代理對象。
- 字段、構造函數
private final Object target;//被代理對象(目標對象)
private final Interceptor interceptor;//攔截器
private final Map<Class<?>, Set<Method>> signatureMap; //記錄了@Signature註解中的信息
private Plugin(Object target, Interceptor interceptor, Map<Class<?>, Set<Method>> signatureMap) {
this.target = target;
this.interceptor = interceptor;
this.signatureMap = signatureMap;
}
- wrap()方法
該方法用於創建動態代理對象。
public static Object wrap(Object target, Interceptor interceptor) {
Map<Class<?>, Set<Method>> signatureMap = getSignatureMap(interceptor);
Class<?> type = target.getClass();
Class<?>[] interfaces = getAllInterfaces(type, signatureMap);
if (interfaces.length > 0) {
return Proxy.newProxyInstance(
type.getClassLoader(),
interfaces,
new Plugin(target, interceptor, signatureMap));
}
return target;
}
private static Map<Class<?>, Set<Method>> getSignatureMap(Interceptor interceptor) {
Intercepts interceptsAnnotation = interceptor.getClass().getAnnotation(Intercepts.class);
// issue #251
if (interceptsAnnotation == null) {
throw new PluginException("No @Intercepts annotation was found in interceptor " + interceptor.getClass().getName());
}
Signature[] sigs = interceptsAnnotation.value();
Map<Class<?>, Set<Method>> signatureMap = new HashMap<Class<?>, Set<Method>>();
for (Signature sig : sigs) {
Set<Method> methods = signatureMap.get(sig.type());
if (methods == null) {
methods = new HashSet<Method>();
signatureMap.put(sig.type(), methods);
}
try {
Method method = sig.type().getMethod(sig.method(), sig.args());
methods.add(method);
} catch (NoSuchMethodException e) {
throw new PluginException("Could not find method on " + sig.type() + " named " + sig.method() + ". Cause: " + e, e);
}
}
return signatureMap;
}
private static Class<?>[] getAllInterfaces(Class<?> type, Map<Class<?>, Set<Method>> signatureMap) {
Set<Class<?>> interfaces = new HashSet<Class<?>>();
while (type != null) {
for (Class<?> c : type.getInterfaces()) {
if (signatureMap.containsKey(c)) {
interfaces.add(c);
}
}
type = type.getSuperclass();
}
return interfaces.toArray(new Class<?>[interfaces.size()]);
}
- invoke()方法
該方法其實就是調用被代理對象的方法。首先判斷,該方法是否需要被攔截,如果需要就通過interceptor.intercept()方法進行調用,否則直接調用被代理對象的目標方法即可。通過這裏我們可以發現,其實我們把需要攔截的方法調用放到了interceptor.intercept()方法中進行了實現,這也是爲什麼我們要封裝Invocation對象的原因,因爲這樣的話,方便我們統一處理。
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
try {
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);
} catch (Exception e) {
throw ExceptionUtil.unwrapThrowable(e);
}
}
三、總結
在本章節內容中,我們主要分析了Mybatis中plugin的基本用法和相關源碼,下一章節我們分析Mybatis攔截器在初始化過程中是如何加載的,同時分析如何通過Mybatis的攔截器實現一個簡易的分頁插件。