写在前面

  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. 2-Python基础语法-内存管理-运算符-程序控制

    目录 1 Python 基础语法 1.1 注释 1.2 缩进 1.3 续行 1.4 标识符 1.5 转义序列 1.6 数字 1.7 字符串 1.8 其他 2 Python 运算符 2.1 赋值运算符 ...

  2. utsrelease.h 包含svn信息

    utsrelease.h是一个自动生成的文件,没有办法修改,但这个数据是根据Makefile和.config的内容进行生成的,通过修改这两个文件的内容,可以改变!/usr/src/linux/Make ...

  3. sicily 1017. Rate of Return

    Description Jill has been investing in a mutual fund for a while. Since her income has varied, the a ...

  4. openjudge-NOI 2.6-2988 计算字符串距离

    题目链接:http://noi.openjudge.cn/ch0206/2988/ 题解: 首先,题目有误,少了一个添加操作 和求解LCS之类的思路类似 f[i][j]表示a序列中1..i的部分和b序 ...

  5. ubuntu中安装软件包问题 ------有一些软件包无法被安装。如果您用的是 unstable 发行版。。。

    在ubuntu中安装软件包提示 有一些软件包无法被安装.如果您用的是 unstable 发行版,这也许是因为系统无法达到您要求的状态造成的.该版本中可能会有一些您需要的软件包尚未被创建或是它们已被从新 ...

  6. leetcode 168. Excel Sheet Column Title 171 Excel Sheet Column Number

    题目 //像10进制一样进行 转换   只是要从0开始记录 class Solution { public: string convertToTitle(int n) { char a; string ...

  7. HDU 1255 覆盖的面积(线段树:扫描线求面积并)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1255 题目大意:给你若干个矩形,让你求这些矩形重叠两次及以上的部分的面积. 解题思路:模板题,跟HDU ...

  8. ISSCC 2017论文导读 Session 14 Deep Learning Processors,A 2.9TOPS/W Deep Convolutional Neural Network

    最近ISSCC2017大会刚刚举行,看了关于Deep Learning处理器的Session 14,有一些不错的东西,在这里记录一下. A 2.9TOPS/W Deep Convolutional N ...

  9. linux中的vim 编辑一行内容,如何使光标快速移动到行首和行尾以及行中间某处啊?

    光标定位G 移至行行首nG 移至第n行行首n+ 移n行行首n- 移n行行首n$ 移n行(1表示本行)行尾0 所行行首$ 所行行尾^ 所行首字母h,j,k,l 左移移移右移H 前屏幕首行行首M 屏幕显示 ...

  10. 开始学习MaxCompute

    https://help.aliyun.com/document_detail/34615.html?spm=a2c4g.11186623.6.688.jVxTMW