ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

mybatis 启动流程源码分析(二)之 Configuration-Properties解析

2020-09-15 19:02:24  阅读:226  来源: 互联网

标签:null resource url 源码 defaults mybatis Configuration root evalNode


一. 配置文件

参考: https://www.cnblogs.com/wanthune/p/13674243.html

二. 源码解析

  1. XMLConfigBuilder 就是解析Xml的主类。
public Configuration parse() {
    if (parsed) {
      throw new BuilderException("Each XMLConfigBuilder can only be used once.");
    }
    parsed = true;
    parseConfiguration(parser.evalNode("/configuration"));
    return configuration;
  }
   // 第一步,解析properites节点
  private void parseConfiguration(XNode root) {
    try {
      //issue #117 read properties first
      propertiesElement(root.evalNode("properties"));
      Properties settings = settingsAsProperties(root.evalNode("settings"));
      loadCustomVfs(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);
    }
  }

   // 解析properties节点
    private void propertiesElement(XNode context) throws Exception {
    if (context != null) {
      // 读取properties节点,返回一个Properties对象
      Properties defaults = context.getChildrenAsProperties();
      // 读取resource属性值
      String resource = context.getStringAttribute("resource");
      // 读取url属性值
      String url = context.getStringAttribute("url");
      // 这儿就是为什么不能在文件中同时配置resource和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.");
      }
      // 优先获取resource,之所以先判断Resource,我觉得是因为本地文件读取的更快。
      if (resource != null) {
        defaults.putAll(Resources.getResourceAsProperties(resource));
      } else if (url != null) {
        // 从远程url中读取文件流
        defaults.putAll(Resources.getUrlAsProperties(url));
      }
      Properties vars = configuration.getVariables();
      if (vars != null) {
        defaults.putAll(vars);
      }
      // 将读取到的属性值放到parser(XPathParser)中
      parser.setVariables(defaults);
      // 同时也放入configuration中。
      configuration.setVariables(defaults);
    }
  }

标签:null,resource,url,源码,defaults,mybatis,Configuration,root,evalNode
来源: https://www.cnblogs.com/wanthune/p/13674761.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有