Mybatis允许用户使用自定义拦截器对Sql语句执行过程中的某一点进行拦截处理。
一、职责链模式
使多个对象都有机会处理请求,从而避免请求的发送者和接受者之间的耦合关系。将这些对象连成一条链,并沿着这条链传递请求,直到有一个对象处理它为止。(这里的对象是handler,有点像流水线作业,链上的每一个节点都有自己的职责,完成自己职责内的事。)

mybatis的插件模块用到了该模式,Interceptor对应Handler接口,PageInterceptor或自定义插件对应ConcreteHandler。
三、插件的工作流程
1.自定义插件只需要实现Interceptor接口便可自动注入到Configuration的interceptorChain中;PageInterceptor通过PageHelperAutoConfiguration引入。
2.Configuration在创建Executor、ParameterHandler、StatementHandler、ResultSetHandler对象时,创建的其代理对象。
public ParameterHandler newParameterHandler(MappedStatement mappedStatement, Object parameterObject, BoundSql boundSql) {
ParameterHandler parameterHandler = mappedStatement.getLang().createParameterHandler(mappedStatement, parameterObject, boundSql);
parameterHandler = (ParameterHandler) interceptorChain.pluginAll(parameterHandler);
return parameterHandler;
}
public ResultSetHandler newResultSetHandler(Executor executor, MappedStatement mappedStatement, RowBounds rowBounds, ParameterHandler parameterHandler,
ResultHandler resultHandler, BoundSql boundSql) {
ResultSetHandler resultSetHandler = new DefaultResultSetHandler(executor, mappedStatement, parameterHandler, resultHandler, boundSql, rowBounds);
resultSetHandler = (ResultSetHandler) interceptorChain.pluginAll(resultSetHandler);
return resultSetHandler;
}
public StatementHandler newStatementHandler(Executor executor, MappedStatement mappedStatement, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {
StatementHandler statementHandler = new RoutingStatementHandler(executor, mappedStatement, parameterObject, rowBounds, resultHandler, boundSql);
statementHandler = (StatementHandler) interceptorChain.pluginAll(statementHandler);
return statementHandler;
}
public Object pluginAll(Object target) {
for (Interceptor interceptor : interceptors) {
target = interceptor.plugin(target);
}
return target;
}
public Object plugin(Object target) {
return Plugin.wrap(target, this);
}
public static Object wrap(Object target, Interceptor interceptor) {
//获取拦截的方法签名信息
Map<Class<?>, Set<Method>> signatureMap = getSignatureMap(interceptor);
Class<?> type = target.getClass();
//拦截器中拦截的方法和target中的方法的交集
Class<?>[] interfaces = getAllInterfaces(type, signatureMap);
//如果target中的方法没有在拦截器指定的拦截方法中,则不进行代理。
if (interfaces.length > 0) {
return Proxy.newProxyInstance(
type.getClassLoader(),
interfaces,
new Plugin(target, interceptor, signatureMap));
}
return target;
}
3.Plugin的invoke方法处理时,如果是被拦截器拦截的方法,进入到相应的拦截器进行处理。
public class Plugin implements InvocationHandler {
//被代理的对象
private final Object target;
//拦截器
private final Interceptor interceptor;
//拦截的方法签名
private final Map<Class<?>, Set<Method>> signatureMap;
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);
}
}
}
四、自定义插件
1.实现Interceptor接口。
2.使用@Intercepts注解,标识需要拦截的方法签名列表。
3.@Signature注解,标识拦截的方法签名。
@Component
@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}),
}
)
@Slf4j
public class MyInterceptor implements Interceptor {
@Override
public Object intercept(Invocation invocation) throws Throwable {
log.info("自定义拦截器。。。");
//到下一个拦截器
return invocation.proceed();
}
}
377

被折叠的 条评论
为什么被折叠?



