天天看點

MyBatis源碼解析(二):配置檔案解析

上一文中提到的兩種方式,其實都是最終得到了Configuration對象,正好可以對應上官方文檔上的Configuration XML小節。

MyBatis源碼解析(二):配置檔案解析

這也大概就是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,是以同名屬性會覆寫)。

繼續閱讀