plugin包主要功能
- 用户可通过编写插件来自行扩展功能,例如分表功能,通过拦截sql语句,重新拼接sql语句,进行分表。
plugin包主要结构
-
可以拦截那些接口对象呢?(也就是上图中的target有哪些呢)如图所示:
-
interceptorChain.pluginAll()方法方法在创建ParameterHandler、ResultSetHandler、StatementHandler、Executor共4个接口对象时调用,其含义为给这些接口对象注册拦截器功能,注意是注册,而不是执行拦截,plugin()方法注册拦截器后,在执行上述4个接口对象内的具体方法时,就会自动触发拦截器的执行,也就是用户自定义插件的执行。
plugin包设计模式
- 装饰设计模式、代理模式,图中有清晰的标注,不再赘述
- 值得学习的是,Plugin类中的signatureMap的设计提高了性能,我们都知道在反射调用量大的时候,效率会明显降低,这个map缓存解决了这个问题。 附上源码包结构做参考:
/**
* @author Clinton Begin
*/
public class Plugin implements InvocationHandler {
private final Object target;
private final Interceptor interceptor;
//Map<@Signature注解所对应的type-类,@Signature注解所对应的method-类中的方法>
private final 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) {
//填充signatureMap
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 {
//虽然反射性能不那么好,但是Mybatis缓存了一个对象的反射结果map,只有在初始化我们自己实现的Interceptor的bean的时候
//也就是上面的wrap方法的时候去初始化这个map,以后每次调用这个invoke的时候从map里取,优雅啊!
//如果没有这个map 每次代理类调用它的方法的时候都要通过反射判断这个方法是否要被拦截增强(也就是getSignatureMap的逻辑每次都要走一遍)
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);
// 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<>();
for (Signature sig : sigs) {
Set<Method> methods = signatureMap.computeIfAbsent(sig.type(), k -> new HashSet<>());
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<>();
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()]);
}
}
- 附上源码结构: