在mybatis-config.xml配置文件中配置plugin結點,好比配置一個自定義的日誌插件LogInterceptor和一個開源的分頁插件PageInterceptor:git
<plugins> <plugin interceptor="com.crx.plugindemo.LogInterceptor"></plugin> <plugin interceptor="com.github.pagehelper.PageInterceptor"> <property name="helperDialect" value="oracle" /> </plugin> </plugins>
藉助責任鏈模式,定義一系列的過濾器,在查詢等方法執行時進行過濾,從而達到控制參數、調整查詢語句和控制查詢結果等做用。下面從插件的加載(初始化)、註冊和調用這三個方面闡述插件的工做原理。github
和其餘配置信息同樣,過濾器的加載也會在myBatis讀取配置文件建立Configuration對象時進行,相應的信息存儲在Configuration的interceptorChain屬性中,InterceptorChain封裝了一個包含Interceptor的list:面試
private final List<Interceptor> interceptors = new ArrayList<>();
在XMLConfigBuilder進行解析配置文件時執行pluginElement方法,生成過濾器實例,並添加到上述list中:sql
private void pluginElement(XNode parent) throws Exception { if (parent != null) { for (XNode child : parent.getChildren()) { String interceptor = child.getStringAttribute("interceptor"); Properties properties = child.getChildrenAsProperties(); Interceptor interceptorInstance = (Interceptor) resolveClass(interceptor).getDeclaredConstructor() .newInstance(); interceptorInstance.setProperties(properties); configuration.addInterceptor(interceptorInstance); } } }
能夠爲Executor、ParameterHandler、ResultSetHandler和StatementHandler四個接口註冊過濾器,註冊的時機也就是這四種接口的實現類的對象的生成時機,好比Executor的過濾器的註冊發生在SqlSessionFactory使用openSession方法構建SqlSession的過程當中(由於SqlSession依賴一個Executor實例),ParameterHandler和StatementHandler的過濾器發生在doQuery等sql執行方法執行時註冊,而ResultHandler的過濾器的註冊則發生在查詢結果返回給客戶端的過程當中。以Executor的過濾器的註冊爲例,通過了這樣的過程:數據庫
如今詳細的分析一下Plugin的wrap這個靜態的包裝方法:數組
public static Object wrap(Object target, Interceptor interceptor) { // 從定義的Interceptor實現類上的註解讀取須要攔截的類、方法 Map<Class<?>, Set<Method>> signatureMap = getSignatureMap(interceptor); // Executor、ParameterHandler、ResultSetHandler、StatementHandler Class<?> type = target.getClass(); // 從當前執行的目標類中進行匹配,過濾出符合當前目標的的過濾器 Class<?>[] interfaces = getAllInterfaces(type, signatureMap); if (interfaces.length > 0) { // 動態代理生成Executor的代理實例 return Proxy.newProxyInstance(type.getClassLoader(), interfaces, new Plugin(target, interceptor, signatureMap)); } return target; }
上述代碼中的getSignatureMap方法是解析Interceptor上面的註解的過程,從註解中讀取出須要攔截的方法,依據@Signature的三個變量類、方法method和參數args就能經過反射惟一的定位一個須要攔截的方法。mybatis
private static Map<Class<?>, Set<Method>> getSignatureMap(Interceptor interceptor) { Intercepts interceptsAnnotation = interceptor.getClass().getAnnotation(Intercepts.class); 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<>(); for (Signature sig : sigs) { Set<Method> methods = signatureMap.computeIfAbsent(sig.type(), k -> new HashSet<>()); 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; }
而getAllInterfaces方法是依據不一樣的目標對象(Executor等四種)進行過濾的過程,只給對應的目標進行註冊:oracle
private static Class<?>[] getAllInterfaces(Class<?> type, Map<Class<?>, Set<Method>> signatureMap) { Set<Class<?>> interfaces = new HashSet<>(); 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()]); }
至此,實際使用的Executor對象將是經過動態代理生成的Plugin實例。app
在第二步中完成了過濾器的註冊,在實際調用Executor時,將由實現了InvocationHandler接口的Plugin實例進行接管,對Executor相應方法方法的調用,將實際上調用動態代理體系下的invoke方法:ide
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)) { Object result=interceptor.intercept(new Invocation(target, method, args)); return result; } return method.invoke(target, args); } catch (Exception e) { throw ExceptionUtil.unwrapThrowable(e); } }
如前所述,插件的工做原理是基於責任鏈模式,能夠註冊多個過濾器,層層包裝,最終由內而外造成了一個近似裝飾器模式的責任鏈,最裏面的基本實現是CachingExecutor:
從InterceptorChain的pluginAll方法能夠看出這個結構的構造過程:
public Object pluginAll(Object target) { for (Interceptor interceptor : interceptors) { // 從這能夠看出過濾器的傳遞的過程:動態代理實例由內而外層層包裝,相似於與裝飾器的結構,基礎 實現是一個Executor target = interceptor.plugin(target); } return target; }
這種由內而外的包裝的棧結構從外向內層層代理調用,完成了責任鏈任務的逐級推送。從這個註冊過程能夠看到,在list中越前面的Interceptor越先被代理,在棧結構中越處於底層,執行的順序越靠後。形成了註冊順序和執行順序相反的現象。
pagehelper是一個實現物理分頁效果的開源插件,而且在底層經過Dialect類適配了不一樣的數據庫,其主要做用是攔截sql查詢,構造一個查詢總數的新的以"_COUNT"結尾的新sql,最終再進行分頁查詢。
定義Interceptor接口的實現類並在其上使用@Intercepts和@Signature註解進行過濾的類和方法,好比定義一個打日誌的插件:
@Intercepts({ @Signature(type = Executor.class, method = "query", args = { MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class }), @Signature(type = Executor.class, method = "query", args = { MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class }), }) public class LogInterceptor implements Interceptor { @Override public Object intercept(Invocation invocation) throws Throwable { System.out.println("進入了自定義的插件過濾器!"); System.out.println("執行的目標是:" + invocation.getTarget()); System.out.println("執行的方法是:" + invocation.getMethod()); System.out.println("執行的參數是:" + invocation.getArgs()); return invocation.proceed(); } }
@Intercepts註解中包含了一個方法簽名數組,即@Signature數組,@Signature有三個屬性,type、method和args分別定義要攔截的類、方法名和參數,這樣就能夠經過反射惟一的肯定了要攔截的方法。type即爲在工做原理分析中提到的Executor、ParameterHandler、ResultSetHandler和StatementHandler,method配置對應接口中的方法。
歡迎關注公衆號:前程有光,領取一線大廠Java面試題總結+各知識點學習思惟導+一份300頁pdf文檔的Java核心知識點總結!