In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)06/01 Report--
MyBatis Configuration has a Plugin configuration, which can be interpreted as "plug-in" according to its name, and this plug-in can be understood as "interceptor" in essence. The term "interceptor" is familiar, and there are "interceptors" in many frameworks. What does this Plugin do? What's the point of talking about interceptors alive? Think about how interceptors work. Plugin uses a very important feature of Java-dynamic proxy. So this Plugin can be understood as, when I call a method, I "intercept" its method to do something I want it to do. It can intercept the following methods:
If you attempt to modify or override the behaviour of a given method, you're likely to break the core of MyBatis. Use custom Plugin interceptors with caution, as they may modify things at the heart of Mybatis. To implement custom Plugins we need to implement the Interceptor interface. This class is not annotated @Intercepts.
1 package day_8_mybatis.util; 2 3 import java.util.Iterator; 4 import java.util.Map; 5 import java.util.Properties; 6 7 import org.apache.ibatis.plugin.Interceptor; 8 import org.apache.ibatis.plugin.Intercepts; 9 import org.apache.ibatis.plugin.Invocation;10 import org.apache.ibatis.plugin.Plugin;11 import org.apache.ibatis.plugin.Signature;12 13 /** 14 * @author turbo15 *16 * October 25, 2016 17 */18@Intercepts ({19 @Signature(20 type = Map.class,21 method = "get",22 args = {Object.class}23 )})24 public class ExamplePlugin implements Interceptor {25 26 /* This method is used to implement intercept logic27 * @see org.apache.ibatis.plugin.Interceptor#intercept(org.apache.ibatis.plugin.Invocation)28 */29 @Override30 public Object intercept(Invocation invocation) throws Throwable {31 32 return "ExamplePlugin";33 }34 35 /* Use the current interceptor to implement a proxy for the target object (dynamic proxy for Java when implemented internally) 36 * @see org.apache.ibatis.plugin.Interceptor#plugin(java.lang.Object)37 */38 @Override39 public Object plugin(Object target) {40 return Plugin.wrap(target, this);41 }42 43 /* This method is the same as setProperties in the custom object factory described in the previous section. When initializing Configuration, pass parameters to this method through the configuration file configuration property and call it. 44 * @see org.apache.ibatis.plugin.Interceptor#setProperties(java.util.Properties)45 */46 @Override47 public void setProperties(Properties properties) { 48 Iterator iterator = properties.keySet().iterator();49 while (iterator.hasNext()){50 String keyValue = String.valueOf(iterator.next());51 System.out.println(properties.getProperty(keyValue));52 }53 }54 55 }
Don't forget to register custom plugins in the configuration file mybatis-config.xml. (The following configuration contains some legacy code, which is the configuration in the previous two sections and can be optionally ignored.)
1 2 5 6 7 8 9 10 11 12 13 14 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
Client-side test code:
1 package day_8_mybatis; 2 3 import java.io.IOException; 4 import java.util.HashMap; 5 import java.util.Map; 6 7 import org.apache.ibatis.session.SqlSession; 8 9 import day_8_mybatis.util.ExamplePlugin;10 import day_8_mybatis.util.SessionFactory;11 12 /** 13 * Client 14 * @author turbo15 *16 * October 25, 2016 17 */18 public class Main {19 20 /** 21 * @param args22 * @throws IOException 23 */24 public static void main(String[] args) throws Exception {25 String resource = "day_8_mybatis/mybatis-config.xml"; //Get mybatis profile path 26 SqlSession sqlSession = SessionFactory.getSqlSession(resource); //Construct SqlSession27 from the SessionFactory utility class (this utility class constructs SessionFactory for itself, i.e. in util package) 28 Map map = new HashMap();29 map = (Map)new ExamplePlugin().plugin(map);30 System.out.println(map.get(""));31 32 }33 34 }
At this point, we have a simple understanding of the Plugin in MyBatis. Interested parties can take a look at the plugin method we called at line 29 of our client-side test code. That is, the static method wrap(Object target, Interceptor interceptor) of the Plugin class is called. Tracing this method will find that this method is Java's dynamic proxy.
1 public static Object wrap(Object target, Interceptor interceptor) 2 { 3 Map signatureMap = getSignatureMap(interceptor); 4 Class type = target.getClass(); 5 Class interfaces[] = getAllInterfaces(type, signatureMap); 6 if(interfaces.length > 0) 7 return Proxy.newProxyInstance(type.getClassLoader(), interfaces, new Plugin(target, interceptor, signatureMap)); //returns proxy class instance 8 else 9 return target;10 }
Dynamic proxies are important, reflection is important. Be sure to understand dynamic proxies and reflection repeatedly, which helps us understand many frameworks source code. This article is just a simple understanding, not too much depth.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.