MyBatis 源码解读(二)自定义变量的加载

在上一篇文章中讲到 MyBatis 配置信息是通过 XMLConfigBuilder.parseConfiguration 解析并将解析后的内容存放到 Configuration 对象中,以便后续的读取和使用。

接下来将逐一查看各项配置的加载过程。

propertiesElement 自定义属性值的加载

解析配置的系统变量
propertiesElement(root.evalNode(“properties”));

首先在 mybatis-config.xml 中加入如下配置

  <properties resource="com/tky/perperties/system.properties">
    <property name="username" value="Kenny" />
  </properties>

添加系统变量配置文件 system.properties

system.username=kenny
system.passowrd=123456

解析完成后会 usernamesystem.uernamesystem.password 三个自定义值添加到系统中,以供后续的读取和使用。
解析代码如下:

private void propertiesElement(XNode context) throws Exception {
  if (context != null) {
    // 获取在元素内声明的变量
    Properties defaults = context.getChildrenAsProperties();
    // 声明变量的资源文件位置
    String resource = context.getStringAttribute("resource");
    // 声明变量的网络资源位置
    String url = context.getStringAttribute("url");
    // url 属性 和 resource 声明不可以同时存在
    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));
    }
    // 获取其他默认的MyBatis变量属性配置
    Properties vars = configuration.getVariables();
    if (vars != null) {
      defaults.putAll(vars);
    }
    // 将所有的变量添加进资源文件解析器,以便解析其他文件时使用
    parser.setVariables(defaults);
    // 将所有变量添加进系统配置,以便在后续运行过程中读取和使用
    configuration.setVariables(defaults);
  }
}

通过 getChildrenAsProperties 读取了在 <properties /> 元素内部声明的属性。代码如下:

	public Properties getChildrenAsProperties() {
		Properties properties = new Properties();
		// 遍历子节点
		for (XNode child : getChildren()) {
			// 读取属性名称
			String name = child.getStringAttribute("name");
			// 读取属性值
			String value = child.getStringAttribute("value");
			if (name != null && value != null) {
				properties.setProperty(name, value);
			}
		}
		return properties;
	}

通过 Resources.getResourceAsProperties 或者 Resources.getUrlAsProperties(url) 读取外部配置的资源,通过代码可以看出 MyBatis 只允许同时使用一种 外部资源的声明的方式,这里笔者使用了第一种方式,有兴趣的可以自行尝试第二种方式的读取。
最后将所有变量添加进系统配置 configuration.setVariables(defaults);,以便在后续运行过程中读取和使用。

测试

	@Test
	public void testProperties() {
		Properties properties = sqlSessionFactory.getConfiguration().getVariables();
		logger.info(properties.toString());
		assertEquals("kenny", properties.get("system.username"));
	}

单元测试完成后,可以在控制台的日志中找到如下输出

PropertiesLoadTest - {system.passowrd=123456, system.username=kenny, username=Kenny}

这些变量就是我们自己定义的属性配置,在 MyBatis的声明周期中可以重复读取和使用。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值