Mybatis源码分析之插件责任链、动态代理

来源:互联网 发布:为什么需要云计算 编辑:程序博客网 时间:2024/05/17 23:57

Mybatis源码分析之插件责任链

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);  }}

Plugin 类实现了JDK动态代理类接口

public class Plugin implements InvocationHandler {//实现jdk动态代理接口  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);        // 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()]);    }}
0 0
原创粉丝点击