优秀的编程知识分享平台

网站首页 > 技术文章 正文

Mybatis设置(1) - Settings解析过程

nanyue 2024-11-21 18:55:33 技术文章 2 ℃

1.XMLConfigBulder解析Settrings

XMLConfigBuilder的parseConfiguration方法中解析settings包括四个步骤

  • 获取Document中的settings节点并转换为Properties对象
  • 调用loadCustomVfs方法设置vfsImpl的值
  • 调用loadCustomLogImpl方法设置logImpl的值
  • 调用settingsElement方法设置settings其他配置的值
 	private void parseConfiguration(XNode root) {
        try {
        	...
            Properties settings = this.settingsAsProperties(root.evalNode("settings"));
            this.loadCustomVfs(settings);
            this.loadCustomLogImpl(settings);
            ...
            this.settingsElement(settings);
            ...
        } catch (Exception var3) {
            throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + var3, var3);
        }
    }

2.Settings节点转换为Properties

在settingsAsProperties方法中通过获取XNode的子节点信息得到settings的Properties对象

然后依次判断属性的配置是否正确

    private Properties settingsAsProperties(XNode context) {
        if (context == null) {
            return new Properties();
        } else {
            Properties props = context.getChildrenAsProperties();
            MetaClass metaConfig = MetaClass.forClass(Configuration.class, this.localReflectorFactory);
            Iterator var4 = props.keySet().iterator();
            Object key;
            do {
                if (!var4.hasNext()) {
                    return props;
                }
                key = var4.next();
            } while(metaConfig.hasSetter(String.valueOf(key)));

            throw new BuilderException("The setting " + key + " is not known.  Make sure you spelled it correctly (case sensitive).");
        }
    }

3.加载vfs

在loadCustomVfs方法中,获取vfsImpl配置的值,如果vfs配置正确,则通过Resources类获得配置的类的class对象,并调用Configuration的setVfsImpl方法进行赋值

    private void loadCustomVfs(Properties props) throws ClassNotFoundException {
        String value = props.getProperty("vfsImpl");
        if (value != null) {
            String[] clazzes = value.split(",");
            String[] var4 = clazzes;
            int var5 = clazzes.length;

            for(int var6 = 0; var6 < var5; ++var6) {
                String clazz = var4[var6];
                if (!clazz.isEmpty()) {
                    Class<? extends VFS> vfsImpl = Resources.classForName(clazz);
                    this.configuration.setVfsImpl(vfsImpl);
                }
            }
        }
    }

4.加载LogImpl

在loadCustomLogImpl方法中,从解析的settings的Properties对象中获取logImpl的值,并调用resolveClass方法得到其class对象,然后调用Configuration的setLogImpl进行赋值

    private void loadCustomLogImpl(Properties props) {
        Class<? extends Log> logImpl = this.resolveClass(props.getProperty("logImpl"));
        this.configuration.setLogImpl(logImpl);
    }

5.其他配置的处理

在settingsElement方法中,依次对各个settings配置进行解析和赋值

6.总结

Mybatis对Settings的解析时,首先将settings配置加载为Properties对象,然后指定解析对应的settings参数,这些参数已经在代码中固定了,然后调用Configuration中各个参数的set方法进行赋值,将settings的值处理后保存在Configuration对象中,每个settings参数对应Configuration的属性

Tags:

最近发表
标签列表