麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 開發 > Java > 正文

mybatis中的擴展實現源碼解析

2024-07-14 08:43:27
字體:
來源:轉載
供稿:網友

前言

最近項目中需要用到mybatis的擴展,就深入看了下mybatis的實現,對其靈活性和擴展性的設計思想還是非常佩服的

首先說一下mybatis的攔截器使用方法:繼承其Intercepter接口,實現org.apache.ibatis.plugin.Interceptor#intercept方法,在其中或者對其要執行的方法進行攔截,或者對返回值進行解析

同時基于org.apache.ibatis.plugin.Interceptsorg.apache.ibatis.plugin.Signature這兩個注解來決定,對哪些執行器的哪些方法進行攔截

先看下攔截器的核心接口

public interface Interceptor {  Object intercept(Invocation invocation) throws Throwable;  Object plugin(Object target);  void setProperties(Properties properties); }

其中intercept方法是核心方法,攔截器的實現,plugin方法是用于配置哪些對哪些執行器進行攔截

繼續看源碼,可以看到mybatis的攔截是使用了jdk的動態代理實現的,本質上是一種代理機制

public class Plugin implements InvocationHandler {  private final Object target; private final Interceptor interceptor; 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) { 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 {  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就是代理類,這個代理類是在org.apache.ibatis.plugin.Interceptor#plugin方法中初始化的(調用org.apache.ibatis.plugin.Plugin#wrap),一個Plugin包含一個Intercepter,以及該Intercepter相關的注解配置信息,當對攔截對象的對應方法進行執行的時候,都會根據這些注解配置來判斷是否需要執行該代理攔截(org.apache.ibatis.plugin.Plugin#invoke

再看下plugin是如何被加載的:

public class InterceptorChain {  private final List<Interceptor> interceptors = new ArrayList<Interceptor>();  public Object pluginAll(Object target) { for (Interceptor interceptor : interceptors) {  target = interceptor.plugin(target); } return target; }  public void addInterceptor(Interceptor interceptor) { interceptors.add(interceptor); }  public List<Interceptor> getInterceptors() { return Collections.unmodifiableList(interceptors); } }

org.apache.ibatis.plugin.Interceptor#plugin是在org.apache.ibatis.plugin.InterceptorChain#pluginAll方法中調用的,我們可以看到,如果一個應用中注冊了多個攔截器,那么實際上是會進行一個for循環的加載,由于上面說到了,加載一次,本質上是對mybatis的執行期進行一次代理包裝,那么加載多次的話,就會代理包裝多次,實際上就是一種多重代理了,這樣就保證了每次調用都會按照代理順序進行調用和返回的處理

可以看到,在做這些mybatis執行器初始化的時候,都會進行攔截器鏈的加載

mybatis,擴展,源碼

至此,mybatis基于jdk動態代理的擴展實現方法就了解清楚了,其靈活性在于,它抽象了執行器的概念,并且攔截器的攔截方法也是固定的,我們可以對不同執行器的不同方法進行攔截,而對這些擴展點進行擴展卻不用寫多個方法實現多個方法,只需要實現一個接口就可以搞定了!

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對VeVb武林網的支持。


注:相關教程知識閱讀請移步到JAVA教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 久久久久久久久久久高潮一区二区 | 国产亚洲黑人性受xxxx精品 | 久草在线免费资源站 | 91在线精品亚洲一区二区 | 男男啪羞羞视频网站 | 成人一级黄色大片 | 性欧美一区| 黄网站免费入口 | 韩国美女一区 | 91一区二区三区久久久久国产乱 | 久久久久一区二区三区四区五区 | 日本免费一区二区三区四区 | 日韩精品中文字幕在线观看 | 精品国产一区二区三区久久久狼牙 | 香蕉视频网站在线观看 | 成人免费毛片在线观看 | 久久久一区二区三区四区 | 中文字幕 在线观看 | 欧美a级在线免费观看 | 久久久久久久久久91 | 国产69精品久久99不卡免费版 | 天堂福利电影 | 天堂精品久久 | 一级做a爱片毛片免费 | 亚洲码无人客一区二区三区 | 青青草好吊色 | wwwav国产| 九九热视频这里只有精品 | 欧美成人黄色小视频 | 久久国产精品二国产精品中国洋人 | 亚洲无马在线观看 | 国产三级午夜理伦三级 | 一级看片免费视频 | 性看小视频 | 免费看日产一区二区三区 | 精品国产一区二区三区在线观看 | 国产一级二级毛片 | 亚洲欧美国产精品va在线观看 | 久久国产精品系列 | 国产精品久久久久久久不卡 | 国产成人精品午夜视频' |