mybatis插件机制

mybatis的插件机制使用动态代理实现,不了解的朋友请先了解代理模式和动态代理;插件本质是功能增强,那么它如果需要对某个方法进行增强,首先要拦截这个方法,其实也就类似于拦截器,mybatis的插件在代码中定义为Interceptor,也就是拦截器;后面统一称作拦截器;

主要 类/接口 和 方法

  • Interceptor
方法 主要功能
Object intercept(Invocation invocation) 拦截器功能具体实现
Object plugin(Object target) 为被代理类生成代理对象
void setProperties(Properties properties) 获取自定义配置
  • InterceptorChain
方法 主要功能
public Object pluginAll(Object target) 生成代理对象,通过代理的方式注入拦截器功能
public void addInterceptor(Interceptor interceptor) 注册插件
...getInterceptors() 获取一个不可修改的拦截器集合
  • Plugin
方法 主要功能
public static Object wrap(Object target, Interceptor interceptor) 使用动态代理生成代理对象
public Object invoke(Object proxy, Method method, Object[] args) 调用拦截器的intercept方法或者直接调用被代理对象的当前方法
...getSignatureMap(Interceptor interceptor) 根据拦截器上的注解反射获取目标方法

mybatis插件机制实现

InterceptorChain中维护了一个拦截器列表,也就说所有的拦截器都要在这里注册;

InterceptorChain中关键的一个方法是pluginAll:

  public Object pluginAll(Object target) {
for (Interceptor interceptor : interceptors) {
target = interceptor.plugin(target);
}
return target;
}

pluginAll方法中是调用了Interceptor的plugin方法,

这个plugin方法是一个覆写方法,需要插件开发者自己实现,但是Mybatis已经提供了相应的实现:

  @Override
public Object plugin(Object target) {
return Plugin.wrap(target, this);
}

打开Plugin这个工具类发现它实现了InvocationHandler接口,再看wrap方法:

public static Object wrap(Object target, Interceptor interceptor) {
//获取此拦截器想要代理的目标方法
Map<Class<?>, Set<Method>> signatureMap = getSignatureMap(interceptor);
Class<?> type = target.getClass();
Class<?>[] interfaces = getAllInterfaces(type, signatureMap);
//判断是否是否实现了接口,如果是则使用jdk动态代理生成代理对象,否则返回原对象
if (interfaces.length > 0) {
return Proxy.newProxyInstance(
type.getClassLoader(),
interfaces,
new Plugin(target, interceptor, signatureMap));
}
return target;
}

实际上就是使用jdk动态代理为target创建了一个代理对象,并且这个被代理的对象必须是一个接口的实现类(jdk动态代理必须有接口),否则不会生成代理对象,也就是说拦截器只能拦截接口的方法;既然是动态代理肯定要看一下invoke方法:

 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
try {
Set<Method> methods = signatureMap.get(method.getDeclaringClass());
//如果当前执行的方法是拦截器想要拦截的方法则执行拦截器intercept方法否则,执行原方法;
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);
}
}

可以看到invoke方法中signatureMap是否有存在当前调用的Method决定了是否调用Interceptor的intercept方法,也就是说Interceptor只对signatureMap中的方法生效,那么再来看signatureMap是怎么来的:

 private static Map<Class<?>, Set<Method>> getSignatureMap(Interceptor interceptor) {
//获取拦截器Intercepts注解
Intercepts interceptsAnnotation = interceptor.getClass().getAnnotation(Intercepts.class);
if (interceptsAnnotation == null) { // issue #251
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>();
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;
}

到这里可以看到,Interceptor需要拦截的方法通过@Intercepts注解申明,这里根据注解中的方法签名(类类型,方法名,方法参数)反射获取具体方法并添加到signatureMap中;Interceptor中的Intercepts注解就是定义需要拦截的方法集合;

那么再回过头看InterceptorChain的pluginAll方法,方法内遍历所有的拦截器并调用plugin方法,实际上就是每个插件都是一层代理,通过多层代理来绑定多个插件;换句话说,某个对象要想被InterceptorChain中的拦截器拦截,那么此对象必须经过InterceptorChain的pluginAll(Object target)方法的包装,当然由于jdk动态代理的关系必须是接口对象;

比如mybatis分页插件:

@SuppressWarnings({"rawtypes", "unchecked"})
@Intercepts(
{
@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}),
@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class}),
}
)
public class PageInterceptor implements Interceptor {
//缓存count查询的ms
protected Cache<CacheKey, MappedStatement> msCountMap = null;
private Dialect dialect;
private String default_dialect_class = "com.github.pagehelper.PageHelper";
private Field additionalParametersField; @Override
public Object intercept(Invocation invocation) throws Throwable {
//分页逻辑
} @Override
public Object plugin(Object target) {
return Plugin.wrap(target, this);
} @Override
public void setProperties(Properties properties) {
//获取配置
} }

通过上文我们知道它需要拦截Executor的query方法,Executor是mybatis运行sql的核心组件,之所以能够被拦截是因为:

在Configuration类中创建Executor之后,有这样一句代码:

executor = (Executor) interceptorChain.pluginAll(executor);

完~

mybatis插件机制的更多相关文章

  1. mybatis插件机制原理

    mybatis插件机制及分页插件原理 参考链接:mybatis插件机制及分页插件原理 如何编写一个自定义mybatis插件 参考链接:mybatis 自定义插件的使用

  2. mybatis插件机制及分页插件原理

    MyBatis 插件原理与自定义插件: MyBatis 通过提供插件机制,让我们可以根据自己的需要去增强MyBatis 的功能.需要注意的是,如果没有完全理解MyBatis 的运行原理和插件的工作方式 ...

  3. MyBatis(八):MyBatis插件机制详解

    MyBatis插件插件机制简介 ​ MyBatis插件其实就是为使用者提供的自行拓展拦截器,主要是为了可以更好的满足业务需要. ​ 在MyBatis中提供了四大核心组件对数据库进行处理,分别是Exec ...

  4. Mybatis插件机制以及PageHelper插件的原理

    首先现在已经有很多Mybatis源码分析的文章,之所以重复造轮子,只是为了督促自己更好的理解源码. 1.先看一段PageHelper拦截器的配置,在mybatis的配置文件<configurat ...

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

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

  6. 精尽MyBatis源码分析 - 插件机制

    该系列文档是本人在学习 Mybatis 的源码过程中总结下来的,可能对读者不太友好,请结合我的源码注释(Mybatis源码分析 GitHub 地址.Mybatis-Spring 源码分析 GitHub ...

  7. MyBatis7:MyBatis插件及示例----打印每条SQL语句及其执行时间

    Plugins 摘一段来自MyBatis官方文档的文字. MyBatis允许你在某一点拦截已映射语句执行的调用.默认情况下,MyBatis允许使用插件来拦截方法调用 Executor(update.q ...

  8. MyBatis 插件 : 打印 SQL 及其执行时间

    Plugins 摘一段来自MyBatis官方文档的文字. MyBatis允许你在某一点拦截已映射语句执行的调用.默认情况下,MyBatis允许使用插件来拦截方法调用: Executor(update. ...

  9. MyBatis插件及示例----打印每条SQL语句及其执行时间

    Plugins 摘一段来自MyBatis官方文档的文字. MyBatis允许你在某一点拦截已映射语句执行的调用.默认情况下,MyBatis允许使用插件来拦截方法调用 Executor(update.q ...

随机推荐

  1. Maven整合SSM测试

    前面也说到了关于SSM的整合,话不多说直接从创建项目开始CRUD之路(参考前面写过的Mybatis和Spring整合,SSM简单整合),这是整个项目的结构 以及最终的结果.(附上下载地址) 一.创建M ...

  2. 【sql注入教程】mysql注入直接getshell

    Mysql注入直接getshell的条件相对来说比较苛刻点把 1:要知道网站绝对路径,可以通过报错,phpinfo界面,404界面等一些方式知道 2:gpc没有开启,开启了单引号被转义了,语句就不能正 ...

  3. Android开发技术周报183学习记录

    Android开发技术周报183学习记录 教程 Android性能优化来龙去脉总结 记录 一.性能问题常见 内存泄漏.频繁GC.耗电问题.OOM问题. 二.导致性能问题的原因 1.人为在ui线程中做了 ...

  4. git小技巧

    1 git提交时如何忽略一些文件: 在git根目录下添加,然后提交,就可以使用了,详细的语法详见 https://github.com/github/gitignore github提供了一个通用的. ...

  5. PostgreSQL踩坑现场

    1.PostgreSQL表名.字段名.别名等大小敏感,默认都会转化成小写形式.如果名字中有大写字母,必须分别添加双引号.在写后台时,注意添加\ 如表名:TestTable中有个字段名userName ...

  6. 如何优雅的关闭golang的channel

    How to Gracefully Close Channels,这篇博客讲了如何优雅的关闭channel的技巧,好好研读,收获良多. 众所周知,在golang中,关闭或者向已关闭的channel发送 ...

  7. [MongoDB]Mongo基本使用

    [MongoDB]Mongo基本使用:   汇总: 1. [MongoDB]安装MongoDB2. [MongoDB]Mongo基本使用:3. [MongoDB]MongoDB的优缺点及与关系型数据库 ...

  8. C# 未能加载文件或程序集或它的某一个依赖项。需要强名称程序集

    Could not load file or assembly 'xxx.xxx.xxx, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' ...

  9. 【新手向】使用nodejs抓取百度贴吧内容

    参考教程:https://github.com/alsotang/node-lessons 1~5节 1. 通过superagent抓取页面内容 superagent .get('http://www ...

  10. MemcachedUI-一款基于.NET MVC编写的Memcached监控软件

    一.起源 服务器上使用了Memcached做缓存加速,但是想查看Memcached状态的时候都只能telnet 127.0.0.1 11211 这样来查看,甚是累人,就想能不能做一款web端的软件,方 ...