mybatis 源碼分析(三) 插件原理
- mybatis 源碼分析(一) Xml解析,容器初始化
- mybatis 源碼分析(二) sql執行路徑分析
- mybatis 源碼分析(三) 攔截器原理
- mybatis 源碼分析(四) 自帶連接池
在使用mybatis過程中 我們可能需要對sql 產生的構建的中間環節 進行一些特殊處理
(比如 更換主從庫連接 自定義分表操作 ….) 這個時候就需要使用到攔截器
Interceptor 官方文檔
/**
* @author Clinton Begin
*/
public interface Interceptor {
Object intercept(Invocation invocation) throws Throwable;
Object plugin(Object target);
void setProperties(Properties properties);
}
在第一章中 已經指明
plugins 插件配置 是 注入到 SqlSessionFactoryBean 實例中的
SqlSessionFactoryBean
public class SqlSessionFactoryBean implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
protected SqlSessionFactory buildSqlSessionFactory() throws IOException {
/** 省略其他代碼 */
if (!isEmpty(this.plugins)) {
for (Interceptor plugin : this.plugins) {
/** 配置類中添加插件實現 往下跟蹤會到 InterceptorChain*/
configuration.addInterceptor(plugin);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Registered plugin: '" + plugin + "'");
}
}
}
}
}
InterceptorChain
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);
}
}
那麼攔截器是在什麼地方使用的呢?
第一章的 DefaultSqlSessionFactory 的 第21行代碼 創建executor
Configuration
public class Configuration {
/** 省略其他代碼 */
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 = (Executor) interceptorChain.pluginAll(executor);
return executor;
}
}
由於 interceptorChain.pluginAll 實際上是循環調用interceptor的plugin方法
所以 我們根據官網提供的代碼 繼續分析
如果搜索下 就會發現 interceptor 在4個地方進行了代理攔截
ExamplePlugin
@Intercepts({@Signature(
type= Executor.class,
method = "update",
args = {MappedStatement.class,Object.class})})
public class ExamplePlugin implements Interceptor {
public Object intercept(Invocation invocation) throws Throwable {
return invocation.proceed();
}
public Object plugin(Object target) {
return Plugin.wrap(target, this);
}
public void setProperties(Properties properties) {
}
}
Plugin
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) {
/**
* 該方法是解析攔截器實現類上面的 Intercepts 註解
* 標明executor中那些方法需要被攔截
*/
Map<Class<?>, Set<Method>> signatureMap = getSignatureMap(interceptor);
Class<?> type = target.getClass();
/** 代理 org.apache.ibatis.executor.Executor 接口
* Plugin 實現了動態代理接口 所以調用 Executor 任何方法
* 都會進入到 invoke裏面
*/
Class<?>[] interfaces = getAllInterfaces(type, signatureMap);
if (interfaces.length > 0) {
return Proxy.newProxyInstance(
type.getClassLoader(),
interfaces,
new Plugin(target, interceptor, signatureMap));
}
return target;
}
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);
}
}
private static Map<Class<?>, Set<Method>> getSignatureMap(Interceptor interceptor) {
//TO DO
return signatureMap;
}
private static Class<?>[] getAllInterfaces(Class<?> type, Map<Class<?>, Set<Method>> signatureMap) {
//TO DO
return interfaces.toArray(new Class<?>[interfaces.size()]);
}
}