MyBatis源码解析(二):配置文件解析

本文继续上篇关于MyBatis源码的解析,主要聚焦于Configuration对象的构建,特别是配置文件的解析过程。文章详细阐述了properties配置的解析步骤,通过官方文档的示例和源代码分析,揭示了属性来源的三个层次:方法参数、resource属性和XML子节点,以及它们之间的优先级关系。
摘要由CSDN通过智能技术生成

上一文中提到的两种方式,其实都是最终得到了Configuration对象,正好可以对应上官方文档上的Configuration XML小节。

这也大概就是Configuration的结构,我们先接着上一篇的代码往下看,

 public Configuration parse() {
    if (parsed) {
      throw new BuilderException("Each XMLConfigBuilder can only be used once.");
    }
    parsed = true;
    parseConfiguration(parser.evalNode("/configuration"));
    return configuration;
  }

private void parseConfiguration(XNode root) {
    try {
      //issue #117 read properties first
      propertiesElement(root.evalNode("properties"));
      Properties settings = settingsAsProperties(root.evalNode("settings"));
      loadCustomVfs(settings);
      loadCustomLogImpl(settings);
      typeAliasesElement(root.evalNode("typeAliases"));
      pluginElement(root.evalNode("plugins"));
      objectFactoryElement(root.evalNode("objectFactory"));
      objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));
      reflectorFactoryElement(root.evalNode("reflectorFactory"));
      settingsElement(settings);
      // read it after objectFactory and objectWrapperFactory issue #631
      environmentsElement(root.evalNode("environments"));
      databaseIdProviderElement(root.evalNode("databaseIdProvider"));
      typeHandlerElement(root.evalNode("typeHandlers"));
      mapperElement(root.evalNode("mappers"));
    } catch (Exception e) {
      throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e);
    }
  }

上述代码很明显的显示了,先是解析xml文件里的<configuration/>标签,然后获得各个配置的解析。这里先来分析一下properties配置的解析过程。

首先看一下官方文档中给的示例,如下:

<properties resource="org/mybatis/example/config.properties">
  <property name="username" value="dev_user"/>
  <property name="password" value="F2Fa3!33TYyg"/>
</properties>

然后我们再看一下propertiesElement()方法的代码:

 private void propertiesElement(XNode context) throws Exception {
    if (context != null) {

      //step1 解析properties子节点
      Properties defaults = context.getChildrenAsProperties();
      String resource = context.getStringAttribute("resource");
      String url = context.getStringAttribute("url");
      if (resource != null && url != null) {
        throw new BuilderException("The properties element cannot specify both a URL and a resource based property file reference.  Please specify one or the other.");
      }

      //step2 解析resource或者url路径上的属性文件
      if (resource != null) {
        defaults.putAll(Resources.getResourceAsProperties(resource));
      } else if (url != null) {
        defaults.putAll(Resources.getUrlAsProperties(url));
      }

      //step3 解析方法中传递的properties值
      Properties vars = configuration.getVariables();
      if (vars != null) {
        defaults.putAll(vars);
      }
      parser.setVariables(defaults);
      configuration.setVariables(defaults);
    }
  }

上面代码中我加了一点注释,可以看出来,属性来源分为三个地方,示例中的子节点,以及resource属性,还有方法中传递的参数。且优先级是  方法中传递的参数 > resource属性 > 子节点(Properties本质上是个Hashtable,所以同名属性会覆盖)。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值