mybatis源码配置文件解析之一:解析properties标签
mybatis作为日常开发的常用ORM框架,在开发中起着很重要的作用,了解其源码对日常的开发有很大的帮助。源码版本为:3-3.4.x,可执行到github进行下载。
从这篇文章开始逐一分析mybatis的核心配置文件(mybatis-config.xml),今天先来看properties标签的解析过程。
一、概述
在单独使用mybatis的时候,mybatis的核心配置文件(mybatis-config.xml)就显的特别重要,是整个mybatis运行的基础,只有把配置文件中的各个标签正确解析后才可以正确使用mybatis,下面看properties标签的配置,properties标签的作用就是加载properties文件或者property标签,下面看其具体配置,实例如下
<properties resource="org/mybatis/example/config.properties">
<property name="username" value="dev_user"/>
<property name="password" value="F2Fa3!33TYyg"/>
</properties>
上面是配置的properties标签的配置,在标签中配置了resource属性和property子标签。下面看具体的解析流程,这里分析properties标签的解析过程,启动流程暂不说,直接看解析的代码。
二、详述
上面,看到了properties标签的配置,下面看其解析方法,这里只粘贴部分代码,下面是parseConfiguration方法的代码,
private void parseConfiguration(XNode root) {
try {
//issue #117 read properties first
//解析properties标签
propertiesElement(root.evalNode("properties"));
//解析settings标签
Properties settings = settingsAsProperties(root.evalNode("settings"));
loadCustomVfs(settings);
//解析别名标签,例<typeAlias alias="user" type="cn.com.bean.User"/>
typeAliasesElement(root.evalNode("typeAliases"));
//解析插件标签
pluginElement(root.evalNode("plugins"));
//解析objectFactory标签,此标签的作用是mybatis每次创建结果对象的新实例时都会使用ObjectFactory,如果不设置
//则默认使用DefaultObjectFactory来创建,设置之后使用设置的
objectFactoryElement(root.evalNode("objectFactory"));
//解析objectWrapperFactory标签
objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));
//解析reflectorFactory标签
reflectorFactoryElement(root.evalNode("reflectorFactory"));
settingsElement(settings);
// read it after objectFactory and objectWrapperFactory issue #631
//解析environments标签
environmentsElement(root.evalNode("environments"));
databaseIdProviderElement(root.evalNode("databaseIdProvider"));
typeHandlerElement(root.evalNode("typeHandlers"));
//解析<mappers>标签
mapperElement(root.evalNode("mappers"));
} catch (Exception e) {
throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e);
}
}
从上面的代码中可以找到下面的代码,即为解析的代码,
propertiesElement(root.evalNode("properties"));
这个方法就是解析properties标签,下面看具体的解析过程。
1、解析子标签和属性
/**
* 解析mybatis-config.xml文件中的properties标签
*<properties resource="org/mybatis/example/config.properties">
*<property name="username" value="dev_user"/>
*<property name="password" value="F2Fa3!33TYyg"/>
*</properties>
*解析步骤:
*1、解析配置的property标签,放到defaults中;
*2、解析resource或url属性,放到defaults中;
*3、获取configuration中的variables变量值,放到defaults中
* @param context
* @throws Exception
*/
private void propertiesElement(XNode context) throws Exception {
if (context != null) {
//1、读取properties标签中的property标签<property name="" value=""/>
Properties defaults = context.getChildrenAsProperties();
//2、读取properties标签中的resource、url属性
String resource = context.getStringAttribute("resource");
String url = context.getStringAttribute("url");
//resource和url属性不能同时出现在properties标签中
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不为空,则解析为properties,放到defaults中,由于defaults是key-value结构,所以会覆盖相同key的值
if (resource != null) {
defaults.putAll(Resources.getResourceAsProperties(resource));
} else if (url != null) {//如果url不为空,则解析为properties,放到defaults中,由于defaults是key-value结构,所以会覆盖相同key的值
defaults.putAll(Resources.getUrlAsProperties(url));
}
//3、获得configuration中的variables变量的值,此变量可以通过SqlSessionFactoryBuilder.build()传入properties属性值
Properties vars = configuration.getVariables();
//如果调用build的时候传入了properties属性,放到defaults中
if (vars != null) {
defaults.putAll(vars);
}
//放到parser和configuration对象中
parser.setVariables(defaults);
configuration.setVariables(defaults);
}
}
从上面的解析过程可以看到,首先解析properties标签的子标签,也就是property标签,通过下面的方法获得,
//1、读取properties标签中的property标签<property name="" value=""/>
Properties defaults = context.getChildrenAsProperties();
解析property标签,并放到Properties对象中。那么是如何防盗Properties对象中的那,在getChildrenAsProperties方法中,
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;
}
可以看出是循环property标签,获得其name和value属性,并放入properties对象中。
接着解析properties的resource和url属性,如下
//2、读取properties标签中的resource、url属性
String resource = context.getStringAttribute("resource");
String url = context.getStringAttribute("url");
分别获得resource和url属性,这里这两个属性都是一个路径。
2、处理属性
下面看这个判断,
//resource和url属性不能同时出现在properties标签中
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.");
}
这个判断表明在properties标签中,resource和url属性不能同时出现。
2.1、处理resource和url属性
下面看resource和url属性的处理,这里resource和url两个属性都是代表的一个路径,所以这里肯定是需要读取相应路径下的文件。
//如果resource不为空,则解析为properties,放到defaults中,由于defaults是key-value结构,所以会覆盖相同key的值
if (resource != null) {
defaults.putAll(Resources.getResourceAsProperties(resource));
} else if (url != null) {//如果url不为空,则解析为properties,放到defaults中,由于defaults是key-value结构,所以会覆盖相同key的值
defaults.putAll(Resources.getUrlAsProperties(url));
}
下面看对resource的处理,调用的Resources.getResourceAsProperties(resource))方法,对resource进行处理,
public static Properties getResourceAsProperties(String resource) throws IOException {
Properties props = new Properties();
InputStream in = getResourceAsStream(resource);
props.load(in);
in.close();
return props;
}
从上面的代码可以看出是要转化为InputStream,最后放到Properties对象中,这里加载文件的详细过程,后面再详细分析。
下面看对url的处理,调用Resources.getUrlAsProperties(url)方法,对url进行处理,
public static Properties getUrlAsProperties(String urlString) throws IOException {
Properties props = new Properties();
InputStream in = getUrlAsStream(urlString);
props.load(in);
in.close();
return props;
}
上面的代码依然是把url代表的文件处理成Properties对象。
2.3、处理已添加的Properties
在上面处理完property子标签、resource和url属性后,还进行了下面的处理,即从configuration中获得properties,
//3、获得configuration中的variables变量的值,此变量可以通过SqlSessionFactoryBuilder.build()传入properties属性值
Properties vars = configuration.getVariables();
//如果调用build的时候传入了properties属性,放到defaults中
if (vars != null) {
defaults.putAll(vars);
}
如果configuration中已经存在properties信息,则取出来,放到defaults中。
2.4、放入configuration对象中
经过上面的处理,最后把所有的properties信息放到configuration中,
//放到parser和configuration对象中
parser.setVariables(defaults);
configuration.setVariables(defaults);
把defaults放到了configuration的variables属性中,代表的是整个mybatis环境中所有的properties信息。这个信息可以在mybatis的配置文件中使用${key}使用,比如,${username},则会从configuration的variables中寻找key为username的属性值,并完成自动属性值替换。
三、总结
上面分析了properties标签的解析过程,先解析property标签,然后是resource、url属性,最后是生成SqlSessionFactory的使用调用SqlSessionFactoryBuilder的build方法,传入的properties,从上面的解析过程,可以知道如果存在重复的键,那么最先解析的会被后面解析的覆盖掉,也就是解析过程是:property子标签-->resource-->url-->开发者设置的,那么覆盖过程为:开发者设置的-->url-->resource-->property子标签,优先级最高的为开发者自己设置的properties属性。
原创不易,有不正之处欢迎指正。
mybatis源码配置文件解析之一:解析properties标签的更多相关文章
- mybatis源码配置文件解析之二:解析settings标签
在前边的博客中分析了mybatis解析properties标签,<mybatis源码配置文件解析之一:解析properties标签>.下面来看解析settings标签的过程. 一.概述 在 ...
- mybatis源码配置文件解析之三:解析typeAliases标签
在前边的博客在分析了mybatis解析settings标签,<mybatis源码配置文件解析之二:解析settings标签>.下面来看解析typeAliases标签的过程. 一.概述 在m ...
- mybatis源码配置文件解析之五:解析mappers标签
在上篇博客中分析了plugins标签,<mybatis源码配置文件解析之四:解析plugins标签>,了解了其使用方式及背后的原理.现在来分析<mappers>标签. 一.概述 ...
- mybatis源码配置文件解析之四:解析plugins标签
在前边的博客在分析了mybatis解析typeAliases标签,<mybatis源码配置文件解析之三:解析typeAliases标签>.下面来看解析plugins标签的过程. 一.概述 ...
- mybatis源码配置文件解析之五:解析mappers标签(解析XML映射文件)
在上篇文章中分析了mybatis解析<mappers>标签,<mybatis源码配置文件解析之五:解析mappers标签>重点分析了如何解析<mappers>标签中 ...
- MyBatis 源码分析 - 映射文件解析过程
1.简介 在上一篇文章中,我详细分析了 MyBatis 配置文件的解析过程.由于上一篇文章的篇幅比较大,加之映射文件解析过程也比较复杂的原因.所以我将映射文件解析过程的分析内容从上一篇文章中抽取出来, ...
- Spring mybatis源码篇章-MybatisDAO文件解析(一)
前言:通过阅读源码对实现机制进行了解有利于陶冶情操,承接前文Spring mybatis源码篇章-SqlSessionFactory 加载指定的mybatis主文件 Mybatis模板文件,其中的属性 ...
- Spring mybatis源码篇章-MybatisDAO文件解析(二)
前言:通过阅读源码对实现机制进行了解有利于陶冶情操,承接前文Spring mybatis源码篇章-MybatisDAO文件解析(一) 默认加载mybatis主文件方式 XMLConfigBuilder ...
- mybatis源码配置文件解析之五:解析mappers标签流程图
前面几篇博客分析了mybatis解析mappers标签的过程,主要分为解析package和mapper子标签.补充一张解析的总体过程流程图,画的不好,多多谅解,感谢.
随机推荐
- php里面的一些面试经典的函数
<?php /* 这是一个多线程的读取解决的函数 @param1 $fle 传入要读取的文件名 */ function filelock($fle){ $fp=fopen($fls,'w+'); ...
- 新手学黑客攻防-黑客攻击电脑方式和认识IP地址
听说过黑客,没见过黑客,从最基础的开始学习,让我能在互联网中保护自己的隐私安全和信息安全. 黑客攻击电脑方式 黑客攻击的方式多种多样,但常见的只有以下几种,基本上每个黑客都会用到: 网络报文嗅探 网络 ...
- 【BIM】BIMFACE中创建矢量文本[下篇]
背景 在上一篇文章中,我们通过THREEJS创建了矢量文本,并添加到了BIMFACE场景中,但是仅仅加入到场景中并不是我们的目的,我们的目的是把这种矢量文本加到指定的构件或者空间上,以此标识该构件或空 ...
- Antd 表格内通过rowClassName实现隔行变色的显示方法(转载)
ant design中 table组件很方便,在项目中遇到了需要实现奇偶行颜色不同以方便阅读的功能,主要用到了rowClassName这一api,通过判断index的奇偶来实现不同的样式分配. row ...
- Javascript十六种常用设计模式
单例模式 何为单例模式,就是无论执行多少次函数,都只会生成一个对象哈哈,看一个简单的demo function Instance(name) { this.name = name; } Instanc ...
- scrapy框架在未登录模式下爬取文本,文件和图片的几点收获
1.什么是API接口? https://baijiahao.baidu.com/s?id=1597881116201407882&wfr=spider&for=pc 2.spider文 ...
- vscode不能打开浏览器(Open browser failed!! Please check if you have installed the browser correctly!)
vscode出现上述问题,我也查了很多相关资料,什么改默认浏览器设置什么的,改配置,改系统环境变量什么的,不但麻烦而且最后都难以成功. 下面分享一个可以解决的最简单办法.那就是:舍弃open in b ...
- 为什么 select count(*) from t,在 InnoDB 引擎中比 MyISAM 慢?
统计一张表的总数量,是我们开发中常有的业务需求,通常情况下,我们都是使用 select count(*) from t SQL 语句来完成.随着业务数据的增加,你会发现这条语句执行的速度越来越慢,为什 ...
- 第十六周Java实验作业
实验十六 线程技术 实验时间 2017-12-8 1.实验目的与要求 (1) 掌握线程概念: 多线程是进程执行过程中产生的多条执行线索,线程是比进程执行更小的单位. 线程不能独立存在,必须存在于进程 ...
- 《2018面向对象程序设计(java)课程学习进度条》
学习收获最大的程序阅读或编程任务 课堂/课余学习时间(小时) 发布博客/评论他人博客数量 (阅读/编写)代码行数 周次 九九乘法表 ...