mybatis中的扩展实现源码解析

所属分类: 软件编程 / java 阅读数: 22
收藏 0 赞 0 分享

前言

最近项目中需要用到mybatis的扩展,就深入看了下mybatis的实现,对其灵活性和扩展性的设计思想还是非常佩服的

首先说一下mybatis的拦截器使用方法:继承其Intercepter接口,实现org.apache.ibatis.plugin.Interceptor#intercept方法,在其中或者对其要执行的方法进行拦截,或者对返回值进行解析

同时基于org.apache.ibatis.plugin.Interceptsorg.apache.ibatis.plugin.Signature这两个注解来决定,对哪些执行器的哪些方法进行拦截

先看下拦截器的核心接口

public interface Interceptor {
 
 Object intercept(Invocation invocation) throws Throwable;
 
 Object plugin(Object target);
 
 void setProperties(Properties properties);
 
}

其中intercept方法是核心方法,拦截器的实现,plugin方法是用于配置哪些对哪些执行器进行拦截

继续看源码,可以看到mybatis的拦截是使用了jdk的动态代理实现的,本质上是一种代理机制

public class Plugin implements InvocationHandler {
 
 private final Object target;
 private final Interceptor interceptor;
 private final 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);
 }
 }
 
 ......
}

mybatis的这个Plugin就是代理类,这个代理类是在org.apache.ibatis.plugin.Interceptor#plugin方法中初始化的(调用org.apache.ibatis.plugin.Plugin#wrap),一个Plugin包含一个Intercepter,以及该Intercepter相关的注解配置信息,当对拦截对象的对应方法进行执行的时候,都会根据这些注解配置来判断是否需要执行该代理拦截(org.apache.ibatis.plugin.Plugin#invoke

再看下plugin是如何被加载的:

public class InterceptorChain {
 
 private final List<Interceptor> interceptors = new ArrayList<Interceptor>();
 
 public Object pluginAll(Object target) {
 for (Interceptor interceptor : interceptors) {
  target = interceptor.plugin(target);
 }
 return target;
 }
 
 public void addInterceptor(Interceptor interceptor) {
 interceptors.add(interceptor);
 }
 
 public List<Interceptor> getInterceptors() {
 return Collections.unmodifiableList(interceptors);
 }
 
}

org.apache.ibatis.plugin.Interceptor#plugin是在org.apache.ibatis.plugin.InterceptorChain#pluginAll方法中调用的,我们可以看到,如果一个应用中注册了多个拦截器,那么实际上是会进行一个for循环的加载,由于上面说到了,加载一次,本质上是对mybatis的执行期进行一次代理包装,那么加载多次的话,就会代理包装多次,实际上就是一种多重代理了,这样就保证了每次调用都会按照代理顺序进行调用和返回的处理

可以看到,在做这些mybatis执行器初始化的时候,都会进行拦截器链的加载

至此,mybatis基于jdk动态代理的扩展实现方法就了解清楚了,其灵活性在于,它抽象了执行器的概念,并且拦截器的拦截方法也是固定的,我们可以对不同执行器的不同方法进行拦截,而对这些扩展点进行扩展却不用写多个方法实现多个方法,只需要实现一个接口就可以搞定了!

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。

更多精彩内容其他人还在看

Collections工具类_动力节点Java学院整理

Collections工具类提供了大量针对Collection/Map的操作。这篇文章主要介绍了Collections工具类_动力节点Java学院整理,需要的朋友可以参考下
收藏 0 赞 0 分享

SpringMVC集成Swagger实例代码

本篇文章主要介绍了SpringMVC集成Swagger实例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

十大常见Java String问题_动力节点Java学院整理

本文介绍Java中关于String最常见的10个问题,需要的朋友参考下吧
收藏 0 赞 0 分享

Java微信公众平台开发(13) 微信JSSDK中Config配置

这篇文章主要为大家详细介绍了Java微信公众平台开发第十三步,微信JSSDK中Config配置,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Java实现一个达达租车系统的步骤详解

这篇文章主要给大家介绍了利用Java实现一个达达租车系统的步骤,文中给出了详细的实现思路和示例代码,并在文末给出了完整的源码供大家学习下载,需要的朋友可以参考借鉴,下面来一起看看吧。
收藏 0 赞 0 分享

Java微信公众平台开发(14) 微信web开发者工具使用

这篇文章主要为大家详细介绍了Java微信公众平台开发第十四步,微信web开发者工具的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Spring Boot整合RabbitMQ实例(Topic模式)

Topic Exchange 转发消息主要是根据通配符。接下来通过本文给大家分享Spring Boot整合RabbitMQ实例(Topic模式),需要的朋友参考下吧
收藏 0 赞 0 分享

Java微信公众平台开发(15) 微信JSSDK的使用

这篇文章主要为大家详细介绍了Java微信公众平台开发第十五步,微信JSSDK的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

java多线程的同步方法实例代码

这篇文章主要介绍了 java多线程的同步方法实例代码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

spring boot整合RabbitMQ实例详解(Fanout模式)

这篇文章主要介绍了spring boot整合RabbitMQ的实例讲解(Fanout模式),非常不错,具有参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多