写在前面

  MyBatsi 的拦截器模式是基于代理的代理模式。并且myBatis 的插件开发也是以拦截器的形式集成到myBatis 当中。

  MyBatis 的拦截器已经插件是在org.apache.ibatis.plugin包下面。

  MyBatis拦截器可以拦截的类,Executor(执行器),ParameterHandler(参数处理器),ResultSetHandler(结果集处理器),StatementHandler(句处理器)。

本文以Executor的插件为例子,来说明MyBatis 插件的工作原理

1.首先分析 产生执行器Excutor 的代码

  通过interceptorChain的拦截器链,当一个执行器有过个拦截器,回产生拦截器链,也就是多重代理对象

   Configuration.java 文件
//产生执行器
public Executor newExecutor(Transaction transaction, ExecutorType executorType) {
executorType = executorType == null ? defaultExecutorType : executorType;
//这句再做一下保护,囧,防止粗心大意的人将defaultExecutorType设成null?
executorType = executorType == null ? ExecutorType.SIMPLE : executorType;
Executor executor;
//然后就是简单的3个分支,产生3种执行器BatchExecutor/ReuseExecutor/SimpleExecutor
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);
}
//如果要求缓存,生成另一种CachingExecutor(默认就是有缓存),装饰者模式,所以默认都是返回CachingExecutor
if (cacheEnabled) {
executor = new CachingExecutor(executor);
}
//此处调用插件,通过插件可以改变Executor行为
executor = (Executor) interceptorChain.pluginAll(executor);
return executor;
}

Executor 执行器采用了模板方法模式。

Executor是执行器调度的核心。SqlSesson对外提供的api 其实就是 对于Executor 的调用(API-->SqlSesson-->Executor-->Statment-->DB)

  模板方法模式(解释):

Executor 接口提供了query、update、commit、rollback 等方法。

实现类BaseExecutor 给予基本的实现,在执行目标之前和之后 做了相关的处理(模板),且提供了实现具体操作的抽象方法。使得具体实现类SimpleExecutor、BatchExecutor、ReuseExecutor关注具体方法的实现。

SimpleExecutor,BatchExecutor、ReuseExecutor 只需要实现自己的具体操作 doQuery、doUpdate、doCommit、doRollBack.

Executor executor = new SimpleExecutor();
executor .query();

2 其实Plugin采用了,插件,用的代理模式。自己实现了InvocationHandler接口,维护了目标类target和一个目标拦截器interceptor  

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
Map<Class<?>, Set<Method>> signatureMap = getSignatureMap(interceptor);
//取得要改变行为的类(ParameterHandler|ResultSetHandler|StatementHandler|Executor)
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)) {
//调用Interceptor.intercept,也即插入了我们自己的逻辑
return interceptor.intercept(new Invocation(target, method, args));
}
//最后还是执行原来逻辑
return method.invoke(target, args);
} catch (Exception e) {
throw ExceptionUtil.unwrapThrowable(e);
}
}
}

myBatis 中用到的设计模式

一、建造者模式

BaseBuilder、XMLMapperBuilder、SQlSessionFactoryBuilder

二、工厂方法

SqlSessionFactory、TranscationFactory、MapperProxyFactory

三、模板方法模式

Executor、StatmentHandler

四、动态代理

Plugin、SqlSessionTemplate、MapperProxy

五、责任链模式

Interceptor、InterceptorChain

  

mybatis源码分析(5)-----拦截器的实现原理(动态代理+责任链)的更多相关文章

  1. springMVC源码分析--HandlerInterceptor拦截器调用过程(二)

    在上一篇博客springMVC源码分析--HandlerInterceptor拦截器(一)中我们介绍了HandlerInterceptor拦截器相关的内容,了解到了HandlerInterceptor ...

  2. springMVC源码分析之拦截器

    一个东西用久了,自然就会从仅使用的层面上升到探究其原理的层面,在javaweb中springmvc更是如此,越是优秀的框架,其底层实现代码更是复杂,而在我看来,一个优秀程序猿就相当于一名武林高手,不断 ...

  3. MyBatis源码分析(各组件关系+底层原理

    MyBatis源码分析MyBatis流程图 下面将结合代码具体分析. MyBatis具体代码分析 SqlSessionFactoryBuilder根据XML文件流,或者Configuration类实例 ...

  4. Mybatis源码分析--关联表查询及延迟加载原理(二)

    在上一篇博客Mybatis源码分析--关联表查询及延迟加载(一)中我们简单介绍了Mybatis的延迟加载的编程,接下来我们通过分析源码来分析一下Mybatis延迟加载的实现原理. 其实简单来说Myba ...

  5. springMVC源码分析--HandlerInterceptor拦截器(一)

    对SpringMVC有所了解的人肯定接触过HandlerInterceptor拦截器,HandlerInterceptor接口给我们提供了3个方法: (1)preHandle: 在执行controll ...

  6. (一) Mybatis源码分析-解析器模块

    Mybatis源码分析-解析器模块 原创-转载请说明出处 1. 解析器模块的作用 对XPath进行封装,为mybatis-config.xml配置文件以及映射文件提供支持 为处理动态 SQL 语句中的 ...

  7. MyBatis源码分析(2)—— Plugin原理

    @(MyBatis)[Plugin] MyBatis源码分析--Plugin原理 Plugin原理 Plugin的实现采用了Java的动态代理,应用了责任链设计模式 InterceptorChain ...

  8. MyBatis 源码分析 - 插件机制

    1.简介 一般情况下,开源框架都会提供插件或其他形式的拓展点,供开发者自行拓展.这样的好处是显而易见的,一是增加了框架的灵活性.二是开发者可以结合实际需求,对框架进行拓展,使其能够更好的工作.以 My ...

  9. MyBatis 源码分析 - 缓存原理

    1.简介 在 Web 应用中,缓存是必不可少的组件.通常我们都会用 Redis 或 memcached 等缓存中间件,拦截大量奔向数据库的请求,减轻数据库压力.作为一个重要的组件,MyBatis 自然 ...

  10. MyBatis 源码分析 - 内置数据源

    1.简介 本篇文章将向大家介绍 MyBatis 内置数据源的实现逻辑.搞懂这些数据源的实现,可使大家对数据源有更深入的认识.同时在配置这些数据源时,也会更清楚每种属性的意义和用途.因此,如果大家想知其 ...

随机推荐

  1. UIScrollViewDelegate 方法调用

    UIScrollViewDelegate 方法调用 /** 设置缩放的View, 初始化完之后调用此方法告诉scrollView 谁可以缩放操作, 然后进行布局 */ func viewForZoom ...

  2. Enumeration的学习

    枚举是jdk5.0之后的新特性.枚举的使用在编程中能起到很大的作用,本文从枚举的适用范围.枚举的特点.枚举的使用等三个方面学习枚举 一.枚举的使适用范围 “在有限的范围内选择值”:比如一个星期只有星期 ...

  3. PTP简介

    PTP简介 在通信网络中,许多业务的正常运行都要求网络时钟同步,即整个网络各设备之间的时间或频率差保持在合理的误差水平内.网络时钟同步包括以下两个概念: l              时间同步:也叫相 ...

  4. 用numpy计算成交量加权平均价格(VWAP),并实现读写文件

    VWAP(Volume-Weighted Average Price,成交量加权平均价格)是一个非常重要的经济学量,它代表着金融资产的“平均”价格.某个价格的成交量越高,该价格所占的权重就越大.VWA ...

  5. day3 作业

    文件操作用户很广泛,我们经常对文件进行操作: global log 127.0.0.1 local2 daemon maxconn log 127.0.0.1 local2 info defaults ...

  6. 添加到sudo组里

    $visudo     //切记,此处没有vi和sudo之间没有空格 1.移动光标,到最后一行(最好是找到root ALL=(ALL) ALL,在下面添加一行) 2.按a,进入append模式3.输入 ...

  7. Java 单例模式的七种写法

    Java 单例模式的七种写法 第一种(懒汉,线程不安全) public class Singleton { private static Singleton instance; private Sin ...

  8. 【ASP.NET】:Ckeditor+Fckeditor的使用

    首先这三个文件:下载ckeditor和ckeditor_aspnet_3.6.4和ckfinder 然后把这三个文件复制到项目根目录下 添加引用CKEditor.NET.dll      CKFind ...

  9. 关于<c:if>没有<c:else>解决方案-转载

    <c:if>没有<c:else>可以用<c:choose>来取代结构: <c:choose> <c:when test=""& ...

  10. seat

    Install packages sudo apt-get update sudo apt-get install -y curl curl -sSL https://git.io/vVHhe | b ...