有了《Mybatis执行SQL的4大基础组件详解》 与 《源码解析MyBatis Sharding-Jdbc SQL语句执行流程详解》两篇文章的铺垫,本文将直奔主题:Mybatis插件机制。java
舒适提示:本文也是以提问式阅读与探究源码的技巧展现。spring
从前面的文章咱们已经知道,Mybatis在执行SQL语句的扩展点为Executor、StatementHandler、ParameterHandler与ResultSetHandler,咱们本节将以Executor为入口,向你们展现Mybatis插件的扩展机制。sql
咱们先来看回顾一下Mybatis Executor的建立入口。apache
public Executor newExecutor(Transaction transaction, ExecutorType executorType) {
executorType = executorType == null ? defaultExecutorType : executorType;
executorType = executorType == null ? ExecutorType.SIMPLE : executorType;
Executor executor;
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);
}
if (cacheEnabled) {
executor = new CachingExecutor(executor);
}
executor = (Executor) interceptorChain.pluginAll(executor); // @1
return executor;
}
复制代码
代码@1,:使用InterceptorChain.pluginAll(executor)进行拆件化处理。数组
思考:使用该方法调用后,会返回一个什么对象呢?如何自定义拆件,自定义插件如何执行呢?mybatis
那接下来咱们带着上述疑问,从InterceptorChain类开始进行深刻学习。并发
从名字上看其大意为拦截器链。app
public Object proceed() throws InvocationTargetException, IllegalAccessException {
return method.invoke(target, args);
}
复制代码
该方法的主要目的就是进行处理链的传播,执行完拦截器的方法后,最终须要调用目标方法的invoke方法。函数
记下来中先重点分析一下InterceptorChain方法的pluginAll方法,由于从开头也知道,Mybatis在建立对象时,是调用该方法,完成目标对象的包装。源码分析
public Object pluginAll(Object target) { // @1
for (Interceptor interceptor : interceptors) { // @2
target = interceptor.plugin(target);
// @3
}
return target;
}
复制代码
代码@1:目标对象,须要被代理的对象。
代码@2:遍历InterceptorChain的拦截器链,分别调用Intercpetor对象的Plugin进行拦截(@3)。
那接下来有三个疑问? 问题1:InterceptorChain中的interceptors是从何时初始化的呢,即拦截链中的拦截器从何而来。 问题2:从前面也得知,不管是建立Executor,仍是建立StatementHandler等,都是调用InterceptorChain#pluginAll方法,那是否是拦截器中的拦截器都会做用与目标对象,这应该是有问题的,该如何处理? 问题3:代理对象是如何建立的。
public void addInterceptor(Interceptor interceptor) {
interceptors.add(interceptor);
}
复制代码
要想知道interceptors是如何初始化的,咱们只须要查看该方法的调用链便可。
一路跟踪到源头,咱们会发如今初始化SqlSessionFactory时,会解析一个标签plugin,就能够得知,会在SqlSessionFacotry的一个属性中配置全部的拦截器。 具体配置示例以下:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="shardingDataSource"/>
<property name="mapperLocations" value="classpath*:META-INF/mybatis/mappers/OrderMapper.xml"/>
<property name="plugins">
<array>
<bean id = "teneantInteceptor" class="com.demo.inteceptor.TenaInteceptor"></bean>
</array>
</property>
</bean>
复制代码
问题1已经解决。但后面两个问题彷佛没有什么突破口。因为目前所涉及的三个类,显然不足以给咱们提供答案,咱们先将目光移到InterceptorChain所在包中的其余类,看看其余类的职责如何。
在org.apache.ibatis.plugin中存在以下两个注解类:Intercepts与Signature,从字面意思就是用来配置拦截的方法信息。
备注:至于如何得知上述字段的含义,请看下文的Plugin#getSignatureMap方法。
但另一个类型Plugin类确引发了个人注意。接下来咱们将重点分析Plugin方法。
Plugin的核心属性以下:
private Plugin(Object target, Interceptor interceptor, Map<Class<?>, Set<Method>> signatureMap) {
this.target = target;
this.interceptor = interceptor;
this.signatureMap = signatureMap;
}
复制代码
注意:其构造函数为私有的,那如何构建Plugin呢,其构造方法为Plugin的镜头方法wrap中被调用。
public static Object wrap(Object target, Interceptor interceptor) {
Map<Class<?>, Set<Method>> signatureMap = getSignatureMap(interceptor); // @1
Class<?> type = target.getClass();
Class<?>[] interfaces = getAllInterfaces(type, signatureMap); // @2
if (interfaces.length > 0) {
return Proxy.newProxyInstance( // @3
type.getClassLoader(),
interfaces,
new Plugin(target, interceptor, signatureMap));
}
return target;
}
复制代码
代码@1:获取待包装的Interceptor的方法签名映射表,稍后详细分析。
代码@2:获取须要代理的对象的Class上声明的全部接口。
代码@3:使用JDK内置的Proxy建立代理对象。Proxy建立代理对象的方法声明以下:
public static Object newProxyInstance(ClassLoader loader,Class<?>[] interfaces,InvocationHandler h), 复制代码
注意其事件处理器为Plugin,故在动态运行过程当中会执行Plugin的invoker方法。
在进入Plugin#invoker方法学习以前,咱们先重点查看一下getSignatureMap、getAllInterfaces的实现。
private static Map<Class<?>, Set<Method>> getSignatureMap(Interceptor interceptor) {
Intercepts interceptsAnnotation = interceptor.getClass().getAnnotation(Intercepts.class); // @1
if (interceptsAnnotation == null) { // issue #251 // @2
throw new PluginException("No @Intercepts annotation was found in interceptor " + interceptor.getClass().getName());
}
Signature[] sigs = interceptsAnnotation.value(); // @3
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;
}
复制代码
代码@1:首先从Interceptor的类上获取Intercepts注解。
代码@2:若是Interceptor的类上没有定义Intercepts注解,则抛出异常,说明咱们在自定义插件时,必需要有Intercepts注解。
代码@3:解析Interceptor的values属性(Signature[])数组,而后存入HashMap<Class<?>, Set< Method>>容器内。
舒适提示:从这里能够得知:自定义的插件必须定义Intercepts注解,其注解的value值为Signature。
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()]);
}
复制代码
该方法的实现比较简单,并非获取目标对象所实现的全部接口,而是返回须要拦截的方法所包括的接口。
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { // @1
try {
Set<Method> methods = signatureMap.get(method.getDeclaringClass());
if (methods != null && methods.contains(method)) { // @2
return interceptor.intercept(new Invocation(target, method, args)); // @3
}
return method.invoke(target, args); // @4
} catch (Exception e) {
throw ExceptionUtil.unwrapThrowable(e);
}
}
复制代码
代码@1:首先对其参数列表作一个简单的说明:
代码@2:获取当前执行方法所属的类,并获取须要被拦截的方法集合。
代码@3:若是需被拦截的方法集合包含当前执行的方法,则执行拦截器的interceptor方法。
代码@4:若是不是,则直接调用目标方法的Invoke方法。
从该方法能够看出Interceptor接口的intercept方法就是拦截器自身须要实现的逻辑,其参数为Invocation,在该方法的结束,须要调用invocation#proceed()方法,进行拦截器链的传播。
从目前的学习中,咱们已经了解了Plugin.wrap方法就是生成带来带来类的惟一入口,那该方法在什么地方调用呢?从代码类库中没有找到该方法的调用链,说明该方法是供用户调用的。
再看InterceptorChain方法的pluginAll方法:
public Object pluginAll(Object target) { // @1
for (Interceptor interceptor : interceptors) { // @2
target = interceptor.plugin(target); // @3
}
return target;
}
复制代码
该方法会遍历用户定义的插件实现类(Interceptor),并调用Interceptor的plugin方法,对target进行拆件化处理,即咱们在实现自定义的Interceptor方法时,在plugin中须要根据本身的逻辑,对目标对象进行包装(代理),建立代理对象,那咱们就能够在该方法中使用Plugin#wrap来建立代理类。
接下来咱们再来用序列图来对上述源码分析作一个总结:
问题1:InterceptorChain中的interceptors是从何时初始化的呢,即拦截链中的拦截器从何而来。 答:在初始化SqlSesstionFactory的时候,会解析属性plugins属性,会加载全部的拦截器到InterceptorChain中。
问题2:从前面也得知,不管是建立Executor,仍是建立StatementHandler等,都是调用InterceptorChain#pluginAll方法,那是否是拦截器中的拦截器都会做用与目标对象,这应该是有问题的,该如何处理?
答案是在各自订阅的Interceptor#plugin方法中,咱们能够根据传入的目标对象,是不是该拦截器关注的,若是不关注,则直接返回目标对象,若是关注,则使用Plugin#wrap方法建立代理对象。
问题3:代理对象是如何建立的? 代理对象是使用JDK的动态代理机制建立,使用Plugin#wrap方法建立。
实践是检验真理的惟一标准,那到底如何使用Mybatis的插件机制呢? 建立自定义的拦截器Interceptor,实现Interceptor接口。 1)实现plugin方法,在该方法中决定是否须要建立代理对象,若是建立,使用Plugin#wrap方法建立。 2)实现interceptor方法,该方法中定义拦截器的逻辑,而且在最后请调用invocation.proceed()方法传递拦截器链。 3)使用Intercepts注解,定义须要拦截目标对象的方法签名,支持多个。 将实现的Interceptor在定义SqlSessionFactory的配置中,放入plugins属性。
最后给出一个Mybatis Plugin插件机制使用案例:基于Mycat+Mybatis的多租户方案:基于Mybatis与Mycat的多租户方式,经过Mybatis的插件机制,动态改写SQL语句来实现多租户
做者介绍:《RocketMQ技术内幕》做者,维护公众号:中间件兴趣圈,目前主要发表了源码阅读java集合、JUC(java并发包)、Netty、ElasticJob、Mycat、Dubbo、RocketMQ、mybaits等系列源码。