MyBatis-3.4.2-源码分析4:解析XML之pluginElement(root.evalNode("plugins"))

下面,开始解析plugins的XML

pluginElement(root.evalNode("plugins"))

debug的代码位于

stop in org.apache.ibatis.builder.xml.XMLConfigBuilder.pluginElement

首先关于寻址类的优先级,是可以通过别名来查找的

@SuppressWarnings("unchecked")
	// throws class cast exception as well if types cannot be assigned
	public <T> Class<T> resolveAlias(String string) {
		// 看到这里了
		try {
			if (string == null) {
				return null;
			}
			// issue #748
			//
			String key = string.toLowerCase(Locale.ENGLISH);
			Class<T> value;
			//可以是通过别名寻址
			if (TYPE_ALIASES.containsKey(key)) {
				value = (Class<T>) TYPE_ALIASES.get(key);
			} else {
				//否则正常初始化类
				value = (Class<T>) Resources.classForName(string);
			}
			//返回结果
			return value;
			//结束
		} catch (ClassNotFoundException e) {
			throw new TypeException("Could not resolve type alias '" + string + "'.  Cause: " + e, e);
		}
	}

别名你懂的,如果没有别名,就认为你指定的是一个类,正常初始化这个类

---

private void pluginElement(XNode parent) throws Exception {
		// 如果定义了plugin节点
		if (parent != null) {
			//遍历每1个子节点
			for (XNode child : parent.getChildren()) {
				//获取interceptor的值
				String interceptor = child.getStringAttribute("interceptor");
				//其它子节点作为属性存在
				Properties properties = child.getChildrenAsProperties();
				//指定的interceptor实例化,所以传递的这个类必须实现Interceptor接口
				Interceptor interceptorInstance = (Interceptor) resolveClass(interceptor).newInstance();
				//属性设置,自己实现
				interceptorInstance.setProperties(properties);
				//非常重要
				configuration.addInterceptor(interceptorInstance);
				//看到这里了
			}
		}
	}

 

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

很简单,没啥好说的。

转载于:https://my.oschina.net/qiangzigege/blog/868043

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值