国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - mybatis中的擴展實現源碼解析

mybatis中的擴展實現源碼解析

2021-06-27 15:11此生重演 Java教程

這篇文章主要介給大家紹了關于mybatis中擴展實現的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

前言

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

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

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

先看下攔截器的核心接口

?
1
2
3
4
5
6
7
8
9
public interface interceptor {
 
 object intercept(invocation invocation) throws throwable;
 
 object plugin(object target);
 
 void setproperties(properties properties);
 
}

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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是如何被加載的:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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動態代理的擴展實現方法就了解清楚了,其靈活性在于,它抽象了執行器的概念,并且攔截器的攔截方法也是固定的,我們可以對不同執行器的不同方法進行攔截,而對這些擴展點進行擴展卻不用寫多個方法實現多個方法,只需要實現一個接口就可以搞定了!

總結

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

原文鏈接:http://www.cnblogs.com/cishengchongyan/p/10208040.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 色婷婷网| www.成人 | 国产精品美女久久久久久免费 | 一级大片免费观看 | 91精品一区二区三区久久久久久 | 日韩欧美在线视频 | 综合久久网 | 麻豆国产一区二区三区 | av中文字幕在线 | 亚洲一区中文字幕在线观看 | 久久视频一区 | 成人爽a毛片一区二区免费 日韩av高清在线 | 国产精品久久久久久久久久久久午夜片 | 免费无遮挡www小视频 | 欧美人成在线视频 | 欧美成人自拍 | 在线欧美日韩 | 亚洲一区二区在线视频 | 欧美成人精品一区二区三区 | 一区二区av | 天天干夜夜操 | 久久国产精品免费一区二区三区 | 日日久 | 精品美女在线观看视频在线观看 | 久久午夜精品 | 免费在线a | 色片在线观看 | 欧美午夜一区二区三区免费大片 | 拍真实国产伦偷精品 | 日韩在线观看中文字幕 | 日韩国产欧美视频 | 成年人黄色免费视频 | 性色国产| 中文字幕视频在线观看 | 午夜视频在线 | 国产欧美在线观看 | 黄视频免费观看网站 | 欧美福利网址 | 日韩精品影视 | 久久免费国产 | 婷婷在线视频 |