编程语言
首页 > 编程语言> > mybatis源码学习(四)

mybatis源码学习(四)

作者:互联网

mybatis全局配置文件加载

在上一篇文章中,提到了SqlSessionFactoryBuilder通过调用XMLConfigBuilder里面的parse方法加载配置文件里面的各种资源。在parse方法中,主要调用parseConfiguration方法来加载配置信息,parseConfiguration方法如下:

	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);
	  }
	}
  1. propertiesElement,这个方法是专门用来加载properties变量信息的,这些变量信息包括url,driver,username,password等信息。它是通过读取配置文件里面<properties 这个节点的信息去加载的。
  private void propertiesElement(XNode context) throws Exception {
    if (context != null) {
      //
      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.");
      }
      if (resource != null) {
        defaults.putAll(Resources.getResourceAsProperties(resource));
      } else if (url != null) {
        defaults.putAll(Resources.getUrlAsProperties(url));
      }
      Properties vars = configuration.getVariables();
      if (vars != null) {
        defaults.putAll(vars);
      }
      parser.setVariables(defaults);
      configuration.setVariables(defaults);
    }
  }

从上面的代码上可以看到,它读取resource或者是url的值,然后根据这个值去加载相关的配置信息,因为有些配置像这样的

<configuration>
    <properties resource="jdbc.properties"  />
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${driver}"/>
                <property name="url" value="${url}"/>
                <property name="username" value="${username}"/>
                <property name="password" value="${password}"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="mapper/LeaveMapper.xml"></mapper>
    </mappers>
</configuration>

jdbc.propertis文件内容如下

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://192.168.197.205:3306/system_data_permission?characterEncoding=utf8
username=root
password=toor@gzstrong

这种方式就是把数据库相关配置文件信息放在单独的properties里面,系统记载的时候读取配置properties文件的信息然后填充到下面的变量里面。再看propertiesElement方法,再这个方法里面通过Properties defaults = context.getChildrenAsProperties();方法获取这个节点下面的所有子节点信息,因为有些mybatis全局配置文件里面,并不是把配置信息些在properties里面,而是直接写在<properties 这个节点里面,例如:

    <properties>
        <property name="driver" value="driver"/>
        <property name="url" value="url"/>
        <property name="username" value="username"/>
        <property name="password" value="password"/>
    </properties>

接着根据properties节点的属性读取resource的文件名获取properties信息,或者获取url里的信息,在这里,<properties节点里面,resource和url属性不能共存,然后根据配置信息是url或者是resource读取相关的配置信息。读取完了之后,判断原来configuration对象里面是否存在variables属性,这个属性是在一开始初始化的时候根据实际需要设置进去的。
从代码上看来,properties有加载顺序,这个先后顺序是
1、初始化的时候设置的属性
2、resource或者url上配置信息的顺序
3、properties节点本身子节点的顺序。

标签:resource,url,学习,源码,defaults,mybatis,root,properties,evalNode
来源: https://blog.csdn.net/springe/article/details/98968211