上一文中提到的两种方式,其实都是最终得到了Configuration对象,正好可以对应上官方文档上的Configuration XML小节。
这也大概就是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,所以同名属性会覆盖)。