java,mybatis源码学习简单记录


一、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()]);
}

总结

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值