一、mybatis interceptor
1.简单使用
代码如下(示例):
@Intercepts(
@Signature(type = Executor.class, method = "query",
args = {MappedStatement.class, Object.class,
RowBounds.class, ResultHandler.class})
)
public class MyInterceptor implements Interceptor {
public Object intercept(Invocation invocation) throws Throwable {
Object target = invocation.getTarget();
Method method = invocation.getMethod();
Object[] args = invocation.getArgs();
return invocation;
}
public Object plugin(Object target) {
if (target instanceof Executor) {}
if (target instanceof ParameterHandler) {}
if (target instanceof StatementHandler) {}
if (target instanceof ResultSetHandler) {}
return Plugin.wrap(target,this);
}
public void setProperties(Properties properties) {}
}
2.Interceptor 接口
代码如下(示例):
public interface Interceptor {
//使用 Intercepts注解,plguin方法返回Plugin.wrap(),才会执行此方法
Object intercept(Invocation invocation) throws Throwable;
//return Plugin.wrap(target,this)
Object plugin(Object target);
//Spring bean 方式配置时,如果没有配置属性就不会执行下面的 setProperties 方法
void setProperties(Properties properties);
}
3.源码
// Plugin 部分源码
//目标组件 target 对象引用 比如:CacheExecutor
private final Object target;
//自己的interceptor 比如:MyInterceptor
private final Interceptor interceptor;
//保存Intercepts中Signature配置的 方法
private final Map<Class<?>, Set<Method>>
public static Object wrap(Object target, Interceptor interceptor) {
//解析Intercepts注解,获取Signature中配置组件方法(如:Executor中的query)
Map<Class<?>, Set<Method>> signatureMap = getSignatureMap(interceptor);
//获取组件class,如CacheExecutor
Class<?> type = target.getClass();
//获取此组件class实现的所有接口,用于jdk动态代理生成代理类
Class<?>[] interfaces = getAllInterfaces(type, signatureMap);
//如果target组件的class存在与Intercepts配置的Signature 返回代理类
//不存在则返回原来的 组件类,不做处理
if (interfaces.length > 0) {
return Proxy.newProxyInstance(
type.getClassLoader(),
interfaces,
new Plugin(target, interceptor, signatureMap));
}
return target;
}
//解析获取Intercepts,Signature中的
private static Map<Class<?>, Set<Method>> getSignatureMap(Interceptor interceptor) {
Intercepts interceptsAnnotation = interceptor.getClass().getAnnotation(Intercepts.class);
// issue #251
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()]);
}