淺談myBatis中的插件機制
插件的配置與使用
在mybatis-config.xml配置文件中配置plugin結點,比如配置一個自定義的日志插件LogInterceptor和一個開源的分頁插件PageInterceptor:
<plugins> <plugin interceptor='com.crx.plugindemo.LogInterceptor'></plugin> <plugin interceptor='com.github.pagehelper.PageInterceptor'> <property name='helperDialect' value='oracle' /> </plugin></plugins>
插件的工作原理
借助責任鏈模式,定義一系列的過濾器,在查詢等方法執行時進行過濾,從而達到控制參數、調整查詢語句和控制查詢結果等作用。下面從插件的加載(初始化)、注冊和調用這三個方面闡述插件的工作原理。
過濾器的加載(初始化)
和其他配置信息一樣,過濾器的加載也會在myBatis讀取配置文件創建Configuration對象時進行,相應的信息存儲在Configuration的interceptorChain屬性中,InterceptorChain封裝了一個包含Interceptor的list:
private final List<Interceptor> interceptors = new ArrayList<>();
在XMLConfigBuilder進行解析配置文件時執行pluginElement方法,生成過濾器實例,并添加到上述list中:
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就能通過反射唯一的定位一個需要攔截的方法。
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等四種)進行過濾的過程,只給對應的目標進行注冊:
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實例。
過濾器的調用
在第二步中完成了過濾器的注冊,在實際調用Executor時,將由實現了InvocationHandler接口的Plugin實例進行接管,對Executor相應方法方法的調用,將實際上調用動態代理體系下的invoke方法:
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
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 {@Overridepublic 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配置對應接口中的方法。
到此這篇關于淺談myBatis中的插件機制的文章就介紹到這了,更多相關myBatis 插件機制內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章: