这里我会用2个拦截器作为示例,来讲述整个拦截器的运行流程。我们先看下mybatis-config.xml的配置:
<plugins>
<plugin interceptor="test.Interceptor1"/>
<plugin interceptor="test.Interceptor2"/>
</plugins>
配置之后,就可以直接使用了,代码如下:
public class TestInterceptor {
public static void main(String[] args) throws Exception {
Map<String, Object> map=new HashMap<>();
List<Map<String, Object>> list=DBoperate.mysqlQueryList(map);
System.out.println(list);
}
}
@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 Interceptor1 implements Interceptor {
@Override
public Object intercept(Invocation invocation) throws Throwable {
Object[] args = invocation.getArgs();
MappedStatement ms = (MappedStatement)args[0];
Object parameter = args[1];
RowBounds rowBounds = (RowBounds)args[2];
ResultHandler resultHandler = (ResultHandler)args[3];
Executor executor = (Executor)invocation.getTarget();
CacheKey cacheKey;
BoundSql boundSql;
if (args.length == 4) {
boundSql = ms.getBoundSql(parameter);
cacheKey = executor.createCacheKey(ms, parameter, rowBounds, boundSql);
} else {
cacheKey = (CacheKey)args[4];
boundSql = (BoundSql)args[5];
}
System.out.println("Interceptor1");
List resultList = executor.query(ms, parameter, rowBounds, resultHandler, cacheKey, boundSql);
return resultList;
}
}
@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 Interceptor2 implements Interceptor {
@Override
public Object intercept(Invocation invocation) throws Throwable {
Object[] args = invocation.getArgs();
MappedStatement ms = (MappedStatement)args[0];
Object parameter = args[1];
RowBounds rowBounds = (RowBounds)args[2];
ResultHandler resultHandler = (ResultHandler)args[3];
Executor executor = (Executor)invocation.getTarget();
CacheKey cacheKey;
BoundSql boundSql;
if (args.length == 4) {
boundSql = ms.getBoundSql(parameter);
cacheKey = executor.createCacheKey(ms, parameter, rowBounds, boundSql);
} else {
cacheKey = (CacheKey)args[4];
boundSql = (BoundSql)args[5];
}
System.out.println("Interceptor2");
List resultList = executor.query(ms, parameter, rowBounds, resultHandler, cacheKey, boundSql);
return resultList;
}
}
其他配置和一文看懂mybatis底层运行原理解析这篇里面的一样。
首先我们看获取SqlSession的方法
不多做解释,直接看到org.apache.ibatis.session.defaults.DefaultSqlSessionFactory#openSessionFromDataSource方法的74行:
继续往下看 newExecutor 方法的579行:
继续看org.apache.ibatis.plugin.InterceptorChain#pluginAll方法:
可以看到这里会去遍历 interceptors 这个集合。那么 interceptors 这个属性是什么时候初始化的呢?
是在构造 SqlSessionFactory 时初始化的,继续往下 会看到 org.apache.ibatis.builder.xml.XMLConfigBuilder#parseConfiguration 这个方法
上图中可以看到初始化 mybatis-config.xml 时,会解析其中的plugin属性,然后放到 org.apache.ibatis.plugin.InterceptorChain#interceptors 中。
继续看 interceptor 主流程的执行:
上图中可以看到 Interceptor 接口中的 plugin 方法都有一个默认实现:org.apache.ibatis.plugin.Plugin#wrap 方法
继续看 getSignatureMap 方法:
这段代码主要就是获取 Interceptor 实现类上的注解信息,结合 Interceptor1 上注解可以知道,最后 signatureMap 中存储的信息就是key为 Executor.class,value为Executor接口中的2个query方法。
继续看 getAllInterfaces 方法:
代码中能看到会循环遍历type类的父接口,然后判断signatureMap是否包含此类,结果会返回signatureMap中key包含的接口类,也就是Executor接口了。
继续往下看 Proxy.newProxyInstance 方法:
这行代码就用到了动态代理设计模式了,不熟悉的同学可以先去看看我的白话解析动态代理设计模式。
结果就是会根据mybatis-config.xml中配置的拦截器数量来生成代理类,最后都会去调用org.apache.ibatis.plugin.Plugin#invoke方法
上图中就会去执行自定义Interceptor中的 intercept 方法了。
现在我们假设 Interceptor1 和 Interceptor2 生成的代理类分别为 $ProxyInterceptor1 和 $ProxyInterceptor2 ,它们之间的类图关系如下:
最后的执行流程图如下:
从上图可以看到2个拦截器的执行都是有规律的,每个拦截器的执行流程都是 $ProxyInterceptor.query() ——> Plugin.query() ——> Interceptor.intercept() ,而且是按照拦截器配置顺序的倒序执行的 。当所有拦截器执行完成后,就会去调用目标方法 Executor.query()。
总结:
mybatis拦截器的整个执行流程用到了责任链+动态代理+多态来实现的,需要对责任链和动态代理设计模式有深刻的理解,可以更容易熟悉拦截器的设计方式。