mybatis-plugin包閱讀

plugin包主要功能

  • 用戶可通過編寫插件來自行擴展功能,例如分表功能,通過攔截sql語句,重新拼接sql語句,進行分表。

plugin包主要結構

plugin圖解

  • 可以攔截那些接口對象呢?(也就是上圖中的target有哪些呢)如圖所示:
    可攔截的接口

  • interceptorChain.pluginAll()方法方法在創建ParameterHandler、ResultSetHandler、StatementHandler、Executor共4個接口對象時調用,其含義爲給這些接口對象註冊攔截器功能,注意是註冊,而不是執行攔截,plugin()方法註冊攔截器後,在執行上述4個接口對象內的具體方法時,就會自動觸發攔截器的執行,也就是用戶自定義插件的執行。

plugin包設計模式

  • 裝飾設計模式、代理模式,圖中有清晰的標註,不再贅述
  • 值得學習的是,Plugin類中的signatureMap的設計提高了性能,我們都知道在反射調用量大的時候,效率會明顯降低,這個map緩存解決了這個問題。 附上源碼包結構做參考:
/**
 * @author Clinton Begin
 */
public class Plugin implements InvocationHandler {

  private final Object target;
  private final Interceptor interceptor;
  //Map<@Signature註解所對應的type-類,@Signature註解所對應的method-類中的方法>
  private final Map<Class<?>, Set<Method>> signatureMap;

  private Plugin(Object target, Interceptor interceptor, Map<Class<?>, Set<Method>> signatureMap) {
    this.target = target;
    this.interceptor = interceptor;
    this.signatureMap = signatureMap;
  }

  public static Object wrap(Object target, Interceptor interceptor) {
    //填充signatureMap
    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;
  }

  @Override
  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    try {
      //雖然反射性能不那麼好,但是Mybatis緩存了一個對象的反射結果map,只有在初始化我們自己實現的Interceptor的bean的時候
      //也就是上面的wrap方法的時候去初始化這個map,以後每次調用這個invoke的時候從map裏取,優雅啊!
      //如果沒有這個map 每次代理類調用它的方法的時候都要通過反射判斷這個方法是否要被攔截增強(也就是getSignatureMap的邏輯每次都要走一遍)
      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);
    }
  }

  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<>();
    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;
  }

  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()]);
  }

}
  • 附上源碼結構:
    plugin
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章