Mybatis 源码解析 -插件模块
2017-07-10 12:46
405 查看
MyBatis 允许你在已映射语句执行过程中的某一点进行拦截调用。默认情况下,MyBatis 允许使用插件来拦截的方法调用包括:
这些类中方法的细节可以通过查看每个方法的签名来发现,或者直接查看 MyBatis 的发行包中的源代码。 假设你想做的不仅仅是监控方法的调用,那么你应该很好的了解正在重写的方法的行为。 因为如果在试图修改或重写已有方法的行为的时候,你很可能在破坏 MyBatis 的核心模块。 这些都是更低层的类和方法,所以使用插件的时候要特别当心。——Mybatis
Mybatis插件采用责任链模式,通过动态代理织入实现代码拦截,达到插件的作用!
通过 MyBatis 提供的强大机制,使用插件是非常简单的,只需实现 Interceptor 接口,并指定了想要拦截的方法签名即可,当然还需要配置文件指定插件路径
先来看下配置
插件接口
配置解析
来看下Mybatis四大接口初始化很重要的一个方法interceptorChain.pluginAll(Object target);
来看下这方法发生了什么
来看看Plugin类
下一篇,我们来做个分页例子!
// MyBatis执行器,是MyBatis 调度的核心,负责SQL语句的生成和查询缓存的维护 Executor (update, query, flushStatements, commit, rollback, getTransaction, close, isClosed) //负责对用户传递的参数转换成JDBC Statement 所需要的参数, ParameterHandler (getParameterObject, setParameters) //负责将JDBC返回的ResultSet结果集对象转换成List类型的集合 ResultSetHandler (handleResultSets, handleOutputParameters) //封装了JDBC Statement操作,负责对JDBC statement 的操作,如设置参数、将Statement结果集转换成List集合。 StatementHandler (prepare, parameterize, batch, update, query)
这些类中方法的细节可以通过查看每个方法的签名来发现,或者直接查看 MyBatis 的发行包中的源代码。 假设你想做的不仅仅是监控方法的调用,那么你应该很好的了解正在重写的方法的行为。 因为如果在试图修改或重写已有方法的行为的时候,你很可能在破坏 MyBatis 的核心模块。 这些都是更低层的类和方法,所以使用插件的时候要特别当心。——Mybatis
Mybatis插件采用责任链模式,通过动态代理织入实现代码拦截,达到插件的作用!
通过 MyBatis 提供的强大机制,使用插件是非常简单的,只需实现 Interceptor 接口,并指定了想要拦截的方法签名即可,当然还需要配置文件指定插件路径
先来看下配置
<plugins> // <plugin interceptor="Test.ExecutorPlugin"> <property name="someProperty" value="100"/> </plugin> </plugins>
插件接口
public interface Interceptor { //插件入口,实现插件自己的逻辑,最后执行invocation.proceed()方法,实际就是调用method.invoke(target, args)方法,调用代理类 Object intercept(Invocation invocation) throws Throwable; //符合条件返回代理类,否则返回target Object plugin(Object target); //属性注入 void setProperties(Properties properties); }
配置解析
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(); //注入参数数据 这里跟插件对应 interceptorInstance.setProperties(properties); configuration.addInterceptor(interceptorInstance); } } }
来看下Mybatis四大接口初始化很重要的一个方法interceptorChain.pluginAll(Object target);
来看下这方法发生了什么
//要么返回代理类,要么返回原目标类 public Object pluginAll(Object target) { //代理链的生成,也就是说这里可是实现多个拦截,后面依次执行 for (Interceptor interceptor : interceptors) { target = interceptor.plugin(target); } return target; } public Object plugin(Object target) { if (target instanceof ResultSetHandler) { //核心方法 return Plugin.wrap(target, this); } else { return target; } }
来看看Plugin类
//实现了InvocationHandler接口,不用说 用来做代理的 public class Plugin implements InvocationHandler { //目标对象 private Object target; //拦截类 private Interceptor interceptor; //签名对象 private 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); } } private static Map<Class<?>, Set<Method>> getSignatureMap(Interceptor interceptor) { //获取Intercepts注解 Intercepts interceptsAnnotation = interceptor.getClass().getAnnotation(Intercepts.class); //看来注解还不能空呢,不然抛出异常 if (interceptsAnnotation == null) { throw new PluginException("No @Intercepts annotation was found in interceptor " + interceptor.getClass().getName()); } //获取签名数据 Signature[] sigs = interceptsAnnotation.value(); Map<Class<?>, Set<Method>> signatureMap = new HashMap<Class<?>, Set<Method>>(); //循环获取签名 for (Signature sig : sigs) { Set<Method> methods = signatureMap.get(sig.type()); if (methods == null) { methods = new HashSet<Method>(); //key为签名类型,value为签名方法 signatureMap.put(sig.type(), methods); } try { //只获取符合参数条件的方法 Method method = sig.type().getMethod(sig.method(), sig.args()); methods.add(method); } catch (NoSuchMethodException e) { throw new PluginException("Could not find method on " + sig.type() + " named " + sig.method() + ". Cause: " + e, e); } } return signatureMap; } //获得所有实现的接口 private static Class<?>[] getAllInterfaces(Class<?> type, Map<Class<?>, Set<Method>> signatureMap) { Set<Class<?>> interfaces = new HashSet<Class<?>>(); //循环获取 while (type != null) { for (Class<?> c : type.getInterfaces()) { //只添加签名包含的接口 if (signatureMap.containsKey(c)) { interfaces.add(c); } } type = type.getSuperclass(); } return interfaces.toArray(new Class<?>[interfaces.size()]); } }
下一篇,我们来做个分页例子!
相关文章推荐
- Mybatis 源码解析 -插件模块-分页例子
- Mybatis 源码解析 -事务模块
- MyBatis源码解析(十)——Type类型模块之类型处理器TypeHandler
- MyBatis源码解析(六)——DataSource数据源模块之池型数据源
- MyBatis源码解析(十二)——binding绑定模块之MapperRegisty
- MyBatis源码解析(七)——DataSource数据源模块之托管数据源
- MyBatis源码解析(十一)——Parsing解析模块之通用标记解析器(GenericTokenParser)与标记处理器(TokenHandler)
- MyBatis源码解析(九)——Type类型模块之类型处理器注册器(TypeHandlerRegistry)
- MyBatis源码解析(十)——Type类型模块之类型处理器TypeHandler
- MyBatis源码解析(五)——DataSource数据源模块之非池型数据源
- MyBatis源码解析(八)——Type类型模块之TypeAliasRegistry(类型别名注册器)
- thttpd源码解析 定时器模块
- Android FM 模块学习之四 源码解析(1)
- thttpd源码解析 定时器模块
- ngx lua模块源码简单解析
- AndroidFm模块学习之四源码解析(十一)
- Android FM模块学习之四源码解析(三)
- 源码解析DLL自卸载无模块注入
- Mybatis SqlSessionTemplate 源码解析
- nginx源码分析--nginx模块解析