Home Java javaTutorial [MyBatis source code analysis] Plug-in implementation principle

[MyBatis source code analysis] Plug-in implementation principle

Jun 26, 2017 am 09:35 AM
mybatis analyze accomplish plug-in Source code

MyBatis plug-in principle----start with analysis

##This article analyzes the plug-in implementation principle of MyBatis. Before that, , if you are not very familiar with the MyBatis plug-in, you can refer to this article MyBatis7: MyBatis plug-in and examples ---- print each SQL statement and its execution time. In this article, I use an example to explain what the MyBatis plug-in is and how to implement it. Since the MyBatis plug-in has penetrated into the underlying code of MyBatis, to use the plug-in better, you must be familiar with the plug-in implementation principle and the underlying code of MyBatis. This article analyzes the plug-in implementation principle of MyBatis.

First, we start with plug-in parsing. The source code is located in the pluginElement method of XMLConfigBuilder:

 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 }
Copy after login

Here is the interceptor attribute in the tag. This is the full path of the custom interceptor. The code in line 6 generates an interceptor instance through reflection.

## Then take all the tags under the tag, parse the name and value attributes into a Properties, and set the Properties to the interceptor.

Finally, set the interceptor to Configuration through the code on line 8. The source code is implemented as:

<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>
Copy after login
InterceptorChain is an interceptor chain that stores all defined interceptors and several related operation methods:

 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 }
Copy after login
There are adding interceptors and The target object has three methods: add all interceptors and obtain all current interceptors.

MyBatis plug-in principle----pluginAll method to add plug-ins

We are above I saw a pluginAll method in InterceptorChain. The

pluginAll method generates a proxy for the target object. Then when the target object calls the method, it does not use the original method but the proxy method . This will be explained later. .

MyBatis official website documentation states that plug-ins are allowed to be used at the following four code execution points:

The timing for generating the plug-in (

In other words, the timing of calling the pluginAll method) is Executor, ParameterHandler, ResultSetHandler, StatementHandler When the four interface implementation classes are generated, the timing of each interface implementation class being generated in MyBatis is different. It does not depend on when they were generated. I believe each development tool has a shortcut key to view it. To the place where the pluginAll method is called, the Eclipse I use is Ctrl+Alt+H.

Look at the pluginAll method again:

1 public Object pluginAll(Object target) {2     for (Interceptor interceptor : interceptors) {3       target = interceptor.plugin(target);4     }5     return target;6 }
Copy after login

这里值得注意的是:

  1. 形参Object target,这个是Executor、ParameterHandler、ResultSetHandler、StatementHandler接口的实现类,换句话说,plugin方法是要为Executor、ParameterHandler、ResultSetHandler、StatementHandler的实现类生成代理,从而在调用这几个类的方法的时候,其实调用的是InvocationHandler的invoke方法

  2. 这里的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 }
Copy after login

首先看一下第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 }
Copy after login

看到先拿@Intercepts注解,如果没有定义@Intercepts注解,抛出异常,这意味着使用MyBatis的插件,必须使用注解方式

接着拿到@Intercepts注解下的所有@Signature注解,获取其type属性(表示具体某个接口),再根据method与args两个属性去type下找方法签名一致的方法Method(如果没有方法签名一致的就抛出异常,此签名的方法在该接口下找不到),能找到的话key=type,value=Set,添加到signatureMap中,构建出一个方法签名映射。举个例子来说,就是我定义的@Intercepts注解,Executor下我要拦截的所有Method、StatementHandler下我要拦截的所有Method。

回过头继续看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 }
Copy after login

这里获取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;
Copy after login

如果当前传入的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这个接口
Copy after login

相信这么解释大家应该会明白一点。注意这里生不生成代理,只和接口在不在@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 }
Copy after login

看到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 }
Copy after login

Here, take out the Class corresponding to the method and obtain the method signatures in the Class. In other words, it is Executor, ParameterHandler, ResultSetHandler, and StatementHandler. The @Intercepts annotation defines which method signatures are to be intercepted. .

If the method signature of the currently called method is in the method signature set, that is, it meets the judgment in line 4, then the intercept method of the interceptor is called. Otherwise, the method is called as is and will not be executed. Interceptor.

The above is the detailed content of [MyBatis source code analysis] Plug-in implementation principle. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement dual WeChat login on Huawei mobile phones? How to implement dual WeChat login on Huawei mobile phones? Mar 24, 2024 am 11:27 AM

How to implement dual WeChat login on Huawei mobile phones? With the rise of social media, WeChat has become one of the indispensable communication tools in people's daily lives. However, many people may encounter a problem: logging into multiple WeChat accounts at the same time on the same mobile phone. For Huawei mobile phone users, it is not difficult to achieve dual WeChat login. This article will introduce how to achieve dual WeChat login on Huawei mobile phones. First of all, the EMUI system that comes with Huawei mobile phones provides a very convenient function - dual application opening. Through the application dual opening function, users can simultaneously

What is the Chrome plug-in extension installation directory? What is the Chrome plug-in extension installation directory? Mar 08, 2024 am 08:55 AM

What is the Chrome plug-in extension installation directory? Under normal circumstances, the default installation directory of Chrome plug-in extensions is as follows: 1. The default installation directory location of chrome plug-ins in windowsxp: C:\DocumentsandSettings\username\LocalSettings\ApplicationData\Google\Chrome\UserData\Default\Extensions2. chrome in windows7 The default installation directory location of the plug-in: C:\Users\username\AppData\Local\Google\Chrome\User

Share three solutions to why Edge browser does not support this plug-in Share three solutions to why Edge browser does not support this plug-in Mar 13, 2024 pm 04:34 PM

When users use the Edge browser, they may add some plug-ins to meet more of their needs. But when adding a plug-in, it shows that this plug-in is not supported. How to solve this problem? Today, the editor will share with you three solutions. Come and try it. Method 1: Try using another browser. Method 2: The Flash Player on the browser may be out of date or missing, causing the plug-in to be unsupported. You can download the latest version from the official website. Method 3: Press the "Ctrl+Shift+Delete" keys at the same time. Click "Clear Data" and reopen the browser.

PHP Programming Guide: Methods to Implement Fibonacci Sequence PHP Programming Guide: Methods to Implement Fibonacci Sequence Mar 20, 2024 pm 04:54 PM

The programming language PHP is a powerful tool for web development, capable of supporting a variety of different programming logics and algorithms. Among them, implementing the Fibonacci sequence is a common and classic programming problem. In this article, we will introduce how to use the PHP programming language to implement the Fibonacci sequence, and attach specific code examples. The Fibonacci sequence is a mathematical sequence defined as follows: the first and second elements of the sequence are 1, and starting from the third element, the value of each element is equal to the sum of the previous two elements. The first few elements of the sequence

How to implement the WeChat clone function on Huawei mobile phones How to implement the WeChat clone function on Huawei mobile phones Mar 24, 2024 pm 06:03 PM

How to implement the WeChat clone function on Huawei mobile phones With the popularity of social software and people's increasing emphasis on privacy and security, the WeChat clone function has gradually become the focus of people's attention. The WeChat clone function can help users log in to multiple WeChat accounts on the same mobile phone at the same time, making it easier to manage and use. It is not difficult to implement the WeChat clone function on Huawei mobile phones. You only need to follow the following steps. Step 1: Make sure that the mobile phone system version and WeChat version meet the requirements. First, make sure that your Huawei mobile phone system version has been updated to the latest version, as well as the WeChat App.

Master how Golang enables game development possibilities Master how Golang enables game development possibilities Mar 16, 2024 pm 12:57 PM

In today's software development field, Golang (Go language), as an efficient, concise and highly concurrency programming language, is increasingly favored by developers. Its rich standard library and efficient concurrency features make it a high-profile choice in the field of game development. This article will explore how to use Golang for game development and demonstrate its powerful possibilities through specific code examples. 1. Golang’s advantages in game development. As a statically typed language, Golang is used in building large-scale game systems.

PHP Game Requirements Implementation Guide PHP Game Requirements Implementation Guide Mar 11, 2024 am 08:45 AM

PHP Game Requirements Implementation Guide With the popularity and development of the Internet, the web game market is becoming more and more popular. Many developers hope to use the PHP language to develop their own web games, and implementing game requirements is a key step. This article will introduce how to use PHP language to implement common game requirements and provide specific code examples. 1. Create game characters In web games, game characters are a very important element. We need to define the attributes of the game character, such as name, level, experience value, etc., and provide methods to operate these

How to display the source code of PHP code in the browser without being interpreted and executed? How to display the source code of PHP code in the browser without being interpreted and executed? Mar 11, 2024 am 10:54 AM

How to display the source code of PHP code in the browser without being interpreted and executed? PHP is a server-side scripting language commonly used to develop dynamic web pages. When a PHP file is requested on the server, the server interprets and executes the PHP code in it and sends the final HTML content to the browser for display. However, sometimes we want to display the source code of the PHP file directly in the browser instead of being executed. This article will introduce how to display the source code of PHP code in the browser without being interpreted and executed. In PHP, you can use

See all articles