主页 >
新闻资讯 >
软件开发资讯 >
编程相关软件开发相关 >
Mybatis插件原理分析
更新时间:2016-09-20 15:39
发布者:周老师
我们首先介绍一下Mybatis插件相关的几个类,并对源码进行了简单的分析。
Mybatis插件相关的接口或类有:Intercept、InterceptChain、Plugin和Invocation,这几个接口或类实现了整个Mybatis插件流程。
Interceptor:一个接口,是实现自己功能需要实现的接口
源码如下:
-
-
-
-
public interface Interceptor {
-
-
-
Object intercept(Invocation invocation) throws Throwable;
-
-
-
Object plugin(Object target);
-
-
void setProperties(Properties properties);
-
-
}
InterceptorChain:有一个List<Interceptor> interceptors变量,来保存所有Interceptor的实现类
源码如下:
-
-
-
-
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);
-
}
-
-
}
Invocation:一个比较简单的类,主要的功能就是根据构造函数类执行代理类
源码如下:
-
-
-
-
public class Invocation {
-
-
private Object target;
-
private Method method;
-
private Object[] args;
-
-
public Invocation(Object target, Method method, Object[] args) {
-
this.target = target;
-
this.method = method;
-
this.args = args;
-
}
-
-
public Object getTarget() {
-
return target;
-
}
-
-
public Method getMethod() {
-
return method;
-
}
-
-
public Object[] getArgs() {
-
return args;
-
}
-
-
public Object proceed() throws InvocationTargetException, IllegalAccessException {
-
return method.invoke(target, args);
-
}
-
-
}
Plugin:Mybatis插件的核心类,它实现了代理接口InvocationHandler,是一个代理类
源码详解如下:
-
-
-
-
-
-
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) {
-
-
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);
-
}
-
}
-
-
private static Map<Class<?>, Set<Method>> getSignatureMap(Interceptor interceptor) {
-
-
Intercepts interceptsAnnotation = interceptor.getClass().getAnnotation(Intercepts.class);
-
-
-
if (interceptsAnnotation == null) {
-
throw new PluginException("No @Intercepts annotation was found in interceptor " + interceptor.getClass().getName());
-
}
-
-
Signature[] sigs = interceptsAnnotation.value();
-
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;
-
}
-
-
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()]);
-
}
-
-
}
对Mybatis插件有一定了解的人应该知道,插件拦截的类是Executor、ParameterHandler、ResultSetHandler和StatementHandler的实现类,为什么会这样呢?在Configuration类中看一下代码就明白了,可以看到在初始化Executor、ParameterHandler、ResultSetHandler和StatementHandler都会调用interceptorChain.pluginAll()这个函数,其实这样之后各个接口的实现类就被代理类生成为目标类了。
-
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 Executor newExecutor(Transaction transaction) {
-
return newExecutor(transaction, defaultExecutorType);
-
}
-
-
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);
-
return executor;
-
}
上一篇:
github如何pull最新代码 下一篇:
10个值得推荐的免费设计模板网站