mybatis源码学习3---XMLConfigBuilder解析

1.XMLConfigBuilder

XMLConfigBuilder类位于Mybatis包的org.apache.ibatis.builder.xml目录下,继承于BaseBuilder类,关于BaseBuilder类后续再看。

XMLConfigBuilder看名字能猜到是关于mybatis的XML配置的构造类,负责构造mybatis的XML配置的。

XMLConfigBuilder共有四个属性,代码如下:

1 private boolean parsed;//解析标识,因为Configuration是全局变量,只需要解析创建一次即可,true表示已经解析创建过,false则表示没有
2 private XPathParser parser;
3 private String environment;//环境参数
4 private ReflectorFactory localReflectorFactory = new DefaultReflectorFactory();

XMLConfigBuilder共有6个public构造方法和一个private的构造方法,如下:

public XMLConfigBuilder(Reader reader) {
    this(reader, null, null);
  }

  public XMLConfigBuilder(Reader reader, String environment) {
    this(reader, environment, null);
  }

  public XMLConfigBuilder(Reader reader, String environment, Properties props) {
    this(new XPathParser(reader, true, props, new XMLMapperEntityResolver()), environment, props);
  }

  public XMLConfigBuilder(InputStream inputStream) {
    this(inputStream, null, null);
  }

  public XMLConfigBuilder(InputStream inputStream, String environment) {
    this(inputStream, environment, null);
  }

  public XMLConfigBuilder(InputStream inputStream, String environment, Properties props) {
    this(new XPathParser(inputStream, true, props, new XMLMapperEntityResolver()), environment, props);
  }

  private XMLConfigBuilder(XPathParser parser, String environment, Properties props) {
    super(new Configuration());//调用父类的构造方法
    ErrorContext.instance().resource("SQL Mapper Configuration");
    this.configuration.setVariables(props);
    this.parsed = false;
    this.environment = environment;
    this.parser = parser;
  }

很显然6个public的构造方法都是根据mybatis的配置文件流创建一个XPathParser对象,然后最终都调用了私有的构造方法,而私有的构造方法先是调用了父类BaseBuilder的构造方法,然后分别根据参数给四个属性赋值。

而上一篇文章提到了SqlSessionFactoryBuilder中是通过创建一个XMLConfigBuilder对象,然后调用了对象的parse()方法获取到一个Configuration对象。接下来就先看看XMLConfigBuilder的parse方法,如下:

public Configuration parse() {
    if (parsed) {//判断Configuration是否解析过,Configuration是全局变量,只需要解析创建一次即可
      throw new BuilderException("Each XMLConfigBuilder can only be used once.");
    }
    parsed = true;
    parseConfiguration(parser.evalNode("/configuration"));//调用下面的方法,parser.evalNode("/configuration")解析XML配置的configuration节点的内容,得到XNode对象
    return configuration;
  }
  //根据root中存储的是configuration节点的内容
  private void parseConfiguration(XNode root) {
    try {
      Properties settings = settingsAsPropertiess(root.evalNode("settings"));//设置settings配置
      //issue #117 read properties first
      propertiesElement(root.evalNode("properties"));//设置properties配置
      loadCustomVfs(settings);
      typeAliasesElement(root.evalNode("typeAliases"));//设置typeAliases配置
      pluginElement(root.evalNode("plugins"));//设置plugins配置
      objectFactoryElement(root.evalNode("objectFactory"));//设置objectFactory配置
      objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));//设置objectWrapperFactory配置
      reflectorFactoryElement(root.evalNode("reflectorFactory"));//设置reflectFactory配置
      settingsElement(settings);
      // read it after objectFactory and objectWrapperFactory issue #631
      environmentsElement(root.evalNode("environments"));//设置environments配置
      databaseIdProviderElement(root.evalNode("databaseIdProvider"));//设置databaseIdProvider配置
      typeHandlerElement(root.evalNode("typeHandlers"));//设置typeHandlers配置
      mapperElement(root.evalNode("mappers"));//设置mappers配置
    } catch (Exception e) {
      throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e);
    }
  }

可以看出parse的作用是解析mybatis-config.xml的configuration节点的内容,然后挨个赋值给configuration对象的属性;

而XMLConfigBuilder的其他私有方法都是给根据XNode对象(XML配置的configuration节点内容)来给全局配置变量configuration的属性进行赋值,关于Configuration类的解析下一章会解析

总结:XMLConfigBuilder类的作用是根据全局配置文件mybatis-config.xml的流文件进行解析,解析xml中的各个节点,然后创建一个Configuration对象,并将xml中的节点属性赋值给Configuration对象

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值