java delegate怎么寫_美團面試官:你說你們公司的Mybatis分頁插件是你寫的,給我說說它的設計原理?...

2b0d77bb9f25ea0b0798e4e6eb442dfc.png

來源:http://my.oschina.net/zudajun

大多數框架,都支持插件,用戶可通過編寫插件來自行擴展功能,Mybatis也不例外。

我們從插件配置、插件編寫、插件運行原理、插件注冊與執行攔截的時機、初始化插件、分頁插件的原理等六個方面展開闡述。

# 插件配置

Mybatis的插件配置在configuration內部,初始化時,會讀取這些插件,保存于Configuration對象的InterceptorChain中。

<?xml version="1.0" encoding="UTF-8"?>                    
public class Configuration {    protected final InterceptorChain interceptorChain = new InterceptorChain();}

org.apache.ibatis.plugin.InterceptorChain.java源碼。

public class InterceptorChain {  private final List interceptors = new ArrayList();  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 getInterceptors() {    return Collections.unmodifiableList(interceptors);  }}

上面的for循環代表了只要是插件,都會以責任鏈的方式逐一執行(別指望它能跳過某個節點),所謂插件,其實就類似于攔截器。

# 如何編寫一個插件

插件必須實現org.apache.ibatis.plugin.Interceptor接口。

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

intercept()方法:執行攔截內容的地方,比如想收點保護費。由plugin()方法觸發,interceptor.plugin(target)足以證明。

plugin()方法:決定是否觸發intercept()方法。

setProperties()方法:給自定義的攔截器傳遞xml配置的屬性參數。

下面自定義一個攔截器:

@Intercepts({    @Signature(type = Executor.class, method = "query", args = { MappedStatement.class, Object.class,        RowBounds.class, ResultHandler.class }),    @Signature(type = Executor.class, method = "close", args = { boolean.class }) })public class MyBatisInterceptor implements Interceptor {  private Integer value;  @Override  public Object intercept(Invocation invocation) throws Throwable {    return invocation.proceed();  }  @Override  public Object plugin(Object target) {    System.out.println(value);        // Plugin類是插件的核心類,用于給target創建一個JDK的動態代理對象,觸發intercept()方法    return Plugin.wrap(target, this);  }  @Override  public void setProperties(Properties properties) {    value = Integer.valueOf((String) properties.get("value"));  }}

面對上面的代碼,我們需要解決兩個疑問:

1. ?為什么要寫Annotation注解?注解都是什么含義?

答:Mybatis規定插件必須編寫Annotation注解,是必須,而不是可選。

@Intercepts注解:裝載一個@Signature列表,一個@Signature其實就是一個需要攔截的方法封裝。那么,一個攔截器要攔截多個方法,自然就是一個@Signature列表。

type = Executor.class, method = "query", args = { MappedStatement.class, Object.class,?RowBounds.class, ResultHandler.class }

解釋:要攔截Executor接口內的query()方法,參數類型為args列表。

2. Plugin.wrap(target, this)是干什么的?

答:使用JDK的動態代理,給target對象創建一個delegate代理對象,以此來實現方法攔截和增強功能,它會回調intercept()方法。

org.apache.ibatis.plugin.Plugin.java源碼:

public class Plugin implements InvocationHandler {  private Object target;  private Interceptor interceptor;  private Map, Set> signatureMap;  private Plugin(Object target, Interceptor interceptor, Map, Set> signatureMap) {    this.target = target;    this.interceptor = interceptor;    this.signatureMap = signatureMap;  }  public static Object wrap(Object target, Interceptor interceptor) {    Map, Set> signatureMap = getSignatureMap(interceptor);    Class> type = target.getClass();    Class>[] interfaces = getAllInterfaces(type, signatureMap);    if (interfaces.length > 0) {      // 創建JDK動態代理對象      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 methods = signatureMap.get(method.getDeclaringClass());      // 判斷是否是需要攔截的方法(很重要)      if (methods != null && methods.contains(method)) {        // 回調intercept()方法        return interceptor.intercept(new Invocation(target, method, args));      }      return method.invoke(target, args);    } catch (Exception e) {      throw ExceptionUtil.unwrapThrowable(e);    }  }//...}

Map, Set> signatureMap:緩存需攔截對象的反射結果,避免多次反射,即target的反射結果。

所以,我們不要動不動就說反射性能很差,那是因為你沒有像Mybatis一樣去緩存一個對象的反射結果。

判斷是否是需要攔截的方法,這句注釋很重要,一旦忽略了,都不知道Mybatis是怎么判斷是否執行攔截內容的,要記住。

# Mybatis可以攔截哪些接口對象?

public class Configuration {//...public ParameterHandler newParameterHandler(MappedStatement mappedStatement, Object parameterObject, BoundSql boundSql) {    ParameterHandler parameterHandler = mappedStatement.getLang().createParameterHandler(mappedStatement, parameterObject, boundSql);    parameterHandler = (ParameterHandler) interceptorChain.pluginAll(parameterHandler); // 1    return parameterHandler;  }  public ResultSetHandler newResultSetHandler(Executor executor, MappedStatement mappedStatement, RowBounds rowBounds, ParameterHandler parameterHandler,      ResultHandler resultHandler, BoundSql boundSql) {    ResultSetHandler resultSetHandler = new DefaultResultSetHandler(executor, mappedStatement, parameterHandler, resultHandler, boundSql, rowBounds);    resultSetHandler = (ResultSetHandler) interceptorChain.pluginAll(resultSetHandler); // 2    return resultSetHandler;  }  public StatementHandler newStatementHandler(Executor executor, MappedStatement mappedStatement, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {    StatementHandler statementHandler = new RoutingStatementHandler(executor, mappedStatement, parameterObject, rowBounds, resultHandler, boundSql);    statementHandler = (StatementHandler) interceptorChain.pluginAll(statementHandler); // 3    return statementHandler;  }  public Executor newExecutor(Transaction transaction) {    return newExecutor(transaction, defaultExecutorType);  }  public Executor newExecutor(Transaction transaction, ExecutorType executorType) {    executorType = executorType == null ? defaultExecutorType : executorType;    executorType = executorType == null ? ExecutorType.SIMPLE : executorType;    Executor executor;    if (ExecutorType.BATCH == executorType) {      executor = new BatchExecutor(this, transaction);    } else if (ExecutorType.REUSE == executorType) {      executor = new ReuseExecutor(this, transaction);    } else {      executor = new SimpleExecutor(this, transaction);    }    if (cacheEnabled) {      executor = new CachingExecutor(executor);    }    executor = (Executor) interceptorChain.pluginAll(executor); // 4    return executor;  }//...}

Mybatis只能攔截ParameterHandler、ResultSetHandler、StatementHandler、Executor共4個接口對象內的方法。

重新審視interceptorChain.pluginAll()方法:該方法在創建上述4個接口對象時調用,其含義為給這些接口對象注冊攔截器功能,注意是注冊,而不是執行攔截。

攔截器執行時機:plugin()方法注冊攔截器后,那么,在執行上述4個接口對象內的具體方法時,就會自動觸發攔截器的執行,也就是插件的執行。

所以,一定要分清,何時注冊,何時執行。切不可認為pluginAll()或plugin()就是執行,它只是注冊。

#?Invocation

public class Invocation {  private Object target;  private Method method;  private Object[] args;}

intercept(Invocation invocation)方法的參數Invocation ,我相信你一定可以看得懂,不解釋。

# 初始化插件源碼解析

org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XNode)方法部分源碼。

pluginElement(root.evalNode("plugins")); 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).newInstance();        // 這里展示了setProperties()方法的調用時機        interceptorInstance.setProperties(properties);        configuration.addInterceptor(interceptorInstance);      }    }  }

對于Mybatis,它并不區分是何種攔截器接口,所有的插件都是Interceptor,Mybatis完全依靠Annotation去標識對誰進行攔截,所以,具備接口一致性。

# 分頁插件原理

由于Mybatis采用的是邏輯分頁,而非物理分頁,那么,市場上就出現了可以實現物理分頁的Mybatis的分頁插件。

要實現物理分頁,就需要對String sql進行攔截并增強,Mybatis通過BoundSql對象存儲String sql,而BoundSql則由StatementHandler對象獲取。

public interface StatementHandler {     List query(Statement statement, ResultHandler resultHandler) throws SQLException;    BoundSql getBoundSql();}
public class BoundSql {   public String getSql() {    return sql;  }}

因此,就需要編寫一個針對StatementHandler的query方法攔截器,然后獲取到sql,對sql進行重寫增強。

任它天高海闊,任它變化無窮,我們只要懂得原理,再多插件,我們都可以對其投送王之蔑視。

熱文推薦

Java面試題匯總(04)

12月DB-Engines數據庫排名,你猜誰會是第一?

老弟,你連HTTPS 原理都不懂,還給我講“中間人攻擊”,逗我嗎...


f99dde042ce65a81d8173f24284d5704.gif

9f74e7862329077ddff73eeeaca80d55.png

覺得不錯,請給個「在看」

分享給你的朋友!

00034269e361bf5ad6fba3fca120aa37.gif

- End -

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/258836.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/258836.shtml
英文地址,請注明出處:http://en.pswp.cn/news/258836.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

C++標準庫與Java基礎類對照表

一、集合類對象的集合&#xff0c;指如鏈表&#xff0c;數組&#xff0c;隊列這一類的數據結構。在C標準庫中把這些成為Container, 在Java中稱為Collection。CSTL常用Container&#xff1a; vector, list, map, set, queue, stack, priority_queueJava基礎類&#xff1a;ArrayL…

ASP.NET MVC 在控制器中獲取某個視圖動態的HTML代碼

如果我們需要動態的用AJAX從服務器端獲取HTML代碼&#xff0c;拼接字符串是一種不好的方式&#xff0c;所以我們將HTML代碼寫在cshtml文件中&#xff0c;然后通過代碼傳入model&#xff0c;動態獲取cshtml中的HTML代碼 當然&#xff0c;我們想要使用通用的方法去獲取cshtml&…

SharePoint 2013 處理videoplayerpage.aspx下的個人圖片顯示有誤問題

問題&#xff1a;Personal sites photo cant correct display in the videos pageThe url address of personal sites photo is exist surplus characters. The correct situation is just the characters of "%20", not the characters of "%2520".解決方…

Python學習筆記(一)

1.python語言的優點&#xff1a;(1)是一種高級語言&#xff0c;提供大量的庫和內置數據結構(2)書寫簡潔優美 缺點:是解釋性語言&#xff0c;執行速度較慢 2.從官方網站下載的python解釋器為CPython,是一種用C語言編寫的python解釋器。在命令行如果啟動python3&#xff0c;輸入p…

匯編語言基本概念匯總

匯編語言應該是我們如今學的最“低級”的語言了&#xff0c;由于如今不會再有人去學機器語言了。而匯編語言還在一些硬件或者嵌入式設備上使用并開發著。下面資料是為了大學的匯編考試整理的資料&#xff0c;如今與大家分享&#xff0c;希望能給大家提供幫助。 匯編語言程序設計…

MySQL找出鎖等待

1.服務器級別的鎖等待可以通過show processlist看到等待鎖的線程id&#xff0c;但是無法知道究竟哪個線程持有鎖可以通過mysqladmin debug相關等待鎖的線程以及誰持有鎖可以在錯誤日志中找到2.存儲引擎層的鎖等待則比較麻煩&#xff0c;以下是innodb存儲引擎中鎖等待以及哪個線…

C++給函數傳數組參數C++給函數傳數組參數

C給函數傳數組參數 在C中&#xff0c;如果要給一個函數傳入一個數組&#xff0c;一般都是傳入兩個參數一個數組指針和一個數組大小 例&#xff1a; void print_1 (int n, int *datas ) { for (int i 0; i < n; i) { std::cout << datas[i] <<…

clover引導mbr安裝黑蘋果_安裝黑蘋果記錄(一)

從來沒有接觸過macOS(蘋果系統)&#xff0c;孩子說他用的那臺電腦linux不能安裝一些軟件&#xff0c;問我能不能安裝黑蘋果&#xff0c;一些軟件只有win和mac的客戶端&#xff0c;卻沒有linux版本。他知道我不會給他安裝Windows&#xff0c;一個是怕他玩游戲&#xff0c;另外一…

人口變動的經濟分析

佳木斯市人口變動的經濟分析上 人口生產和物質生產&#xff0c;是有史以來人類社會同時存在和同步發展的兩種生產&#xff0c;一切經濟變動都與人口變動有直接或間接的關系&#xff0c;而一切人口變動又可以從經濟變動中找到原因。為此&#xff0c;本文試根據我國、我省和我市第…

【javascript】操作符:一元操作符

一、一元操作符 <script>// var num1,num2,num3; num11;num22; alert(num1num2);//4 在前 alert(num1num2);//4 num11; alert(num1--num2);//3 num1NaN; alert(num1num2);//NaN num1true; alert(num1num2);//3 true轉化成了1 num1fal…

doxygen問題集錦

使用doxygen排版數學公式出錯的解決方法 參考&#xff1a;http://blog.csdn.net/huangbo10/article/details/46801949 問題 今天玩了一下doxygen&#xff0c;碰到一個問題 ! Undefined control sequence. l.77 $ y_{\mbox{test}} \mathbb{E}[y_{\mbox{train}}] x $ ? !…

ubuntu中make頭文件找不到_和平精英:游戲中找不到人怎么辦?這些技巧幫你練出“火眼金睛”...

大家好&#xff0c;歡迎來到《刺激實戰教室》&#xff0c;我是你們的老朋友刺激哥。俗話說&#xff0c;電子競技不需要視力&#xff0c;而這句話也是《和平精英》的一個“痛點”。在《和平精英》這款游戲中&#xff0c;更是如此。在很多時候&#xff0c;敵人就藏在我們的附近&a…

[CDA數據分析師學習之路] 【CDA就業班獨家發布】學習之路

http://bbs.pinggu.org/thread-4677737-1-1.html轉載于:https://blog.51cto.com/xbh1314/1923452

jdk8永久代從方法區移除的驗證

/*** 測試使用jdk8中是否仍然可以使用永久代* jvm options * -Xms20m -Xmx20m -Xmn10m -XX:PermSize10m -XX:MaxPermSize10m -XX:PrintGCDetails* * 結果運行提示&#xff1a;* Java HotSpot(TM) Client VM warning: ignoring option PermSize10m; support was removed in 8.0*…

eureka配置_F版本SpringCloud 5—Eureka集群和自我保護機制

源碼地址&#xff1a;https://gitee.com/bingqilinpeishenme/Java-Tutorials前言上篇文章中&#xff0c;通過代碼搭建了Eureka注冊中心和客戶端&#xff0c;是Eureka的簡單應用&#xff0c;在本文中將會講解更多關于Eureka服務端的應用以及原理。Eureka 自我保護機制進入自我保…

windows共享內存

在windows編程中避免不了使用共享內存&#xff0c;因為他是進程間通信、文件讀取最簡單的方式&#xff0c;有書上還說其他進程間通訊機制如管、油槽、WM_COPYDATA底層也是用的共享內存機制。關于使用方法還是請參考MSDN。 最近聽說使用共享內存不安全&#xff0c;可能會被其他進…

Chrome指令/追蹤Http請求相關

2019獨角獸企業重金招聘Python工程師標準>>> Chrome HTTP抓包&#xff0c;在地址欄輸入 chrome://net-internals/#requests轉載于:https://my.oschina.net/u/2292141/blog/895603

離散卷積與自相關

本文章轉載自&#xff1a;http://www.cnblogs.com/einyboy/archive/2012/12/30/2839633.html 一、 定義 離散信號f(n),g(n)的定義如下&#xff1a; N-----為信號f(n)的長度 s(n)----為卷積結果序列,長度為len(f(n))len(g(n))-1 例&#xff1a; f(n) [1 2 3]; g(n) [2 3 1]; …

PHP文件操作類

<?php /*************************************************************************************** 文件名&#xff1a;File.cls.php 文件簡介&#xff1a;類clsFile的定義&#xff0c;對文件操作的封裝 版本&#xff1a;2.0 最后修改日期&#xff1a;2011-8-23 *******…

excel打開后灰色不顯示內容_Excel二維表轉換,一分鐘就夠

點擊藍字關注我們44個Excel 使用技巧基本方法作為職場人&#xff0c;加班累如狗。如何更輕松的工作并獲得喜人的報酬便是咱們職場人一直追求的“生活哲理”&#xff0c;說到Excel,對于辦公室群體而言實在是太常見不過了&#xff0c;不管做什么&#xff0c;咱們都會跟它打交道&a…