MyBatis プラグインの原理 ----
この記事では、MyBatis にあまり詳しくない方のために、その前に、MyBatis のプラグイン実装原理を分析します。 MyBatis7 :MyBatis プラグインと例 -- 各 SQL ステートメントとその実行時間を出力する この記事では、例を使用して MyBatis プラグインとは何か、およびその方法を説明します。それを実装するために。 MyBatis プラグインは MyBatis の基礎となるコードに浸透しているため、プラグインをより良く使用するには、プラグインの実装原理と MyBatis の基礎となるコードをよく理解しておく必要があります。この記事では、プラグインの実装原理を分析します。マイバティスの。
まず、プラグイン
1 private void pluginElement(XNode parent) throws Exception { 2 if (parent != null) { 3 for (XNode child : parent.getChildren()) { 4 String interceptor = child.getStringAttribute("interceptor"); 5 Properties properties = child.getChildrenAsProperties(); 6 Interceptor interceptorInstance = (Interceptor) resolveClass(interceptor).newInstance(); 7 interceptorInstance.setProperties(properties); 8 configuration.addInterceptor(interceptorInstance); 9 }10 }11 }
ここでは、
最後に、8行目のコードでインターセプタをConfigurationに設定します。 ソースコードは次のように実装されています:
<span style="color: #008080"> 1</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span><span style="color: #000000"> addInterceptor(Interceptor interceptor) {</span><span style="color: #008080"> 2</span> <span style="color: #000000"> interceptorChain.addInterceptor(interceptor);</span><span style="color: #008080"> 3</span> <span style="color: #000000">}</span><span style="color: #008080"><br></span>
InterceptorChainは、定義されたすべてのインターセプタといくつかの関連するインターセプタを格納するインターセプタチェーンです 操作方法:
1 public class InterceptorChain { 2 3 private final List<interceptor> interceptors = new ArrayList<interceptor>(); 4 5 public Object pluginAll(Object target) { 6 for (Interceptor interceptor : interceptors) { 7 target = interceptor.plugin(target); 8 } 9 return target;10 }11 12 public void addInterceptor(Interceptor interceptor) {13 interceptors.add(interceptor);14 }15 16 public List<interceptor> getInterceptors() {17 return Collections.unmodifiableList(interceptors);18 }19 20 }</interceptor></interceptor></interceptor>
インターセプタを追加する、ターゲットオブジェクトにすべてのインターセプタを追加する、および現在のすべてのインターセプタを取得するという 3 つの方法があります。
MyBatis プラグインの原則----pluginAll メソッドがプラグインを追加します
上記では、InterceptorChain の pluginAll メソッドを見てきました。pluginAll メソッドはターゲット オブジェクトのプロキシを生成し、その後対象オブジェクトがメソッドを呼び出す このとき、オリジナルのメソッドではなく、後述するプロキシメソッドを呼び出します。
MyBatis 公式 Web サイトのドキュメントには、次の 4 つのコード実行ポイントでプラグインの使用が許可されていると記載されています:
プラグインを生成するタイミング (他のつまり、pluginAll メソッドは Timing と呼ばれます) Executor、ParameterHandler、ResultSetHandler、StatementHandler の 4 つのインターフェース実装クラスが生成されるタイミングは、MyBatis 内で生成されるタイミングによって異なります。すべての開発ツールには、pluginAll メソッドが呼び出される場所を確認するためのショートカット キーがあると思います。私が使用している Eclipse は Ctrl+Alt+H です。
pluginAll メソッドをもう一度見てください:
1 public Object pluginAll(Object target) {2 for (Interceptor interceptor : interceptors) {3 target = interceptor.plugin(target);4 }5 return target;6 }
这里值得注意的是:
形参Object target,这个是Executor、ParameterHandler、ResultSetHandler、StatementHandler接口的实现类,换句话说,plugin方法是要为Executor、ParameterHandler、ResultSetHandler、StatementHandler的实现类生成代理,从而在调用这几个类的方法的时候,其实调用的是InvocationHandler的invoke方法
这里的target是通过for循环不断赋值的,也就是说如果有多个拦截器,那么如果我用P表示代理,生成第一次代理为P(target),生成第二次代理为P(P(target)),生成第三次代理为P(P(P(target))),不断嵌套下去,这就得到一个重要的结论:
... 中后定义的实际其拦截器方法先被执行 ,因为根据这段代码来看,后定义的代理实际后生成,包装了先生成的代理,自然其代理方法也先执行
plugin方法中调用MyBatis提供的现成的生成代理的方法Plugin.wrap(Object target, Interceptor interceptor),接着我们看下wrap方法的源码实现。
MyBatis插件原理----Plugin的wrap方法的实现
Plugin的wrap方法实现为:
1 public static Object wrap(Object target, Interceptor interceptor) { 2 Map<class>, Set<method>> signatureMap = getSignatureMap(interceptor); 3 Class> type = target.getClass(); 4 Class>[] interfaces = getAllInterfaces(type, signatureMap); 5 if (interfaces.length > 0) { 6 return Proxy.newProxyInstance( 7 type.getClassLoader(), 8 interfaces, 9 new Plugin(target, interceptor, signatureMap));10 }11 return target;12 }</method></class>
首先看一下第2行的代码,获取Interceptor上定义的所有方法签名:
1 private static Map<class>, Set<method>> getSignatureMap(Interceptor interceptor) { 2 Intercepts interceptsAnnotation = interceptor.getClass().getAnnotation(Intercepts.class); 3 // issue #251 4 if (interceptsAnnotation == null) { 5 throw new PluginException("No @Intercepts annotation was found in interceptor " + interceptor.getClass().getName()); 6 } 7 Signature[] sigs = interceptsAnnotation.value(); 8 Map<class>, Set<method>> signatureMap = new HashMap<class>, Set<method>>(); 9 for (Signature sig : sigs) {10 Set<method> methods = signatureMap.get(sig.type());11 if (methods == null) {12 methods = new HashSet<method>();13 signatureMap.put(sig.type(), methods);14 }15 try {16 Method method = sig.type().getMethod(sig.method(), sig.args());17 methods.add(method);18 } catch (NoSuchMethodException e) {19 throw new PluginException("Could not find method on " + sig.type() + " named " + sig.method() + ". Cause: " + e, e);20 }21 }22 return signatureMap;23 }</method></method></method></class></method></class></method></class>
看到先拿@Intercepts注解,如果没有定义@Intercepts注解,抛出异常,这意味着使用MyBatis的插件,必须使用注解方式。
接着拿到@Intercepts注解下的所有@Signature注解,获取其type属性(表示具体某个接口),再根据method与args两个属性去type下找方法签名一致的方法Method(如果没有方法签名一致的就抛出异常,此签名的方法在该接口下找不到),能找到的话key=type,value=Set
回过头继续看wrap方法,在拿到方法签名映射后,调用getAllInterfaces方法,传入的是Target的Class对象以及之前获取到的方法签名映射:
1 private static Class>[] getAllInterfaces(Class> type, Map<class>, Set<method>> signatureMap) { 2 Set<class>> interfaces = new HashSet<class>>(); 3 while (type != null) { 4 for (Class> c : type.getInterfaces()) { 5 if (signatureMap.containsKey(c)) { 6 interfaces.add(c); 7 } 8 } 9 type = type.getSuperclass();10 }11 return interfaces.toArray(new Class>[interfaces.size()]);12 }</class></class></method></class>
这里获取Target的所有接口,如果方法签名映射中有这个接口,那么添加到interfaces中,这是一个Set,最终将Set转换为数组返回。
wrap方法的最后一步:
1 if (interfaces.length > 0) {2 return Proxy.newProxyInstance(3 type.getClassLoader(),4 interfaces,5 new Plugin(target, interceptor, signatureMap));6 }7 return target;
如果当前传入的Target的接口中有@Intercepts注解中定义的接口,那么为之生成代理,否则原Target返回。
这段理论可能大家会看得有点云里雾里,我这里举个例子:
= StatementHandler., method = "query", args = {Statement., ResultHandler.= StatementHandler., method = "update", args = {Statement. org.apache.ibatis.executor.statement.StatementHandler=[ org.apache.ibatis.executor.statement.StatementHandler.update(java.sql. Statement) java.sql.SQLException, java.util.List org.apache.ibatis.executor.statement.StatementHandler.query(java.sql.Statement,org.apache. ibatis.session.ResultHandler) java.sql.SQLException]} 一个Class对应一个Set,Class为StatementHandler.class,Set为StataementHandler中的两个方法 如果我new的是StatementHandler接口的实现类,那么可以为之生成代理,因为signatureMap中的key有StatementHandler这个接口 如果我new的是Executor接口的实现类,那么直接会把Executor接口的实现类原样返回,因为signatureMap中的key并没有Executor这个接口
相信这么解释大家应该会明白一点。注意这里生不生成代理,只和接口在不在@Intercepts中定义过有关,和方法签名无关,具体某个方法走拦截器,在invoke方法中,马上来看一下。
MyBatis插件原理----Plugin的invoke方法
首先看一下Plugin方法的方法定义:
1 public class Plugin implements InvocationHandler { 2 3 private Object target; 4 private Interceptor interceptor; 5 private Map<class>, Set<method>> signatureMap; 6 7 private Plugin(Object target, Interceptor interceptor, Map<class>, Set<method>> signatureMap) { 8 this.target = target; 9 this.interceptor = interceptor;10 this.signatureMap = signatureMap;11 }12 ...13 }</method></class></method></class>
看到Plugin是InvocationHandler接口的实现类,换句话说,为目标接口生成代理之后,最终执行的都是Plugin的invoke方法,看一下invoke方法的实现:
1 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 2 try { 3 Set<method> methods = signatureMap.get(method.getDeclaringClass()); 4 if (methods != null && methods.contains(method)) { 5 return interceptor.intercept(new Invocation(target, method, args)); 6 } 7 return method.invoke(target, args); 8 } catch (Exception e) { 9 throw ExceptionUtil.unwrapThrowable(e);10 }11 }</method>
ここでは、メソッドに対応する Class を取り出し、その Class 内のメソッド シグネチャ、つまり Executor、ParameterHandler、ResultSetHandler、StatementHandler を取得します。 @Intercepts アノテーションは、どのメソッド シグネチャをインターセプトするかを定義します。
現在呼び出されたメソッドのメソッドシグネチャがメソッドシグネチャセットに含まれている場合、つまり4行目の判定を満たしている場合、インターセプタのインターセプトメソッドが呼び出されます。それ以外の場合、メソッドはそのまま呼び出されます。インターセプターは実行されません。
以上が【MyBatisソースコード解析】プラグイン実装原理の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

AI Hentai Generator
AIヘンタイを無料で生成します。

人気の記事

ホットツール

DVWA
Damn Vulnerable Web App (DVWA) は、非常に脆弱な PHP/MySQL Web アプリケーションです。その主な目的は、セキュリティ専門家が法的環境でスキルとツールをテストするのに役立ち、Web 開発者が Web アプリケーションを保護するプロセスをより深く理解できるようにし、教師/生徒が教室環境で Web アプリケーションを教え/学習できるようにすることです。安全。 DVWA の目標は、シンプルでわかりやすいインターフェイスを通じて、さまざまな難易度で最も一般的な Web 脆弱性のいくつかを実践することです。このソフトウェアは、

SublimeText3 中国語版
中国語版、とても使いやすい

SublimeText3 英語版
推奨: Win バージョン、コードプロンプトをサポート!

ZendStudio 13.5.1 Mac
強力な PHP 統合開発環境

PhpStorm Mac バージョン
最新(2018.2.1)のプロフェッショナル向けPHP統合開発ツール
