MyBatis(一) xml文件解析流程 Configuration

1.Configuration




    
        
        
    
    
        
        
        
        
    

 

MyBatis的配置中心的初始化过程
1.触发Configuration的入口

  String resource = "mybatisConfig.xml";
        Reader reader = null;
        try {
            reader = Resources.getResourceAsReader(resource);
        } catch (IOException e) {
            System.out.println(e.getMessage());

        }
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);

2.SqlSessionFactoryBuilder的build方法在获取SqlSessionFactory需要先初始化配置信息,并从配置文件中解析获取初始化SqlSessionFactory的必要信息

 public SqlSessionFactory build(Reader reader, String environment, Properties properties) {
        SqlSessionFactory var5;
        try {
       //此处初始化XMLConfigBuilder,这个类对于Configuration的生成至关重要
            XMLConfigBuilder e = new XMLConfigBuilder(reader, environment, properties);
//调用build方法传入参数中的e.parse()返回结果就是Configuration对象
            var5 = this.build(e.parse());
        } catch (Exception var14) {
            throw ExceptionFactory.wrapException("Error building SqlSession.", var14);
        } finally {
            ErrorContext.instance().reset();

            try {
                reader.close();
            } catch (IOException var13) {
                ;
            }

        }

        return var5;
    }

3.XMLConfigBuilder的parse方法负责调用parseConfiguration方法

public Configuration parse() {
        if(this.parsed) {
            throw new BuilderException("Each XMLConfigBuilder can only be used once.");
        } else {
            this.parsed = true;
            this.parseConfiguration(this.parser.evalNode("/configuration"));
            return this.configuration;
        }
    }

4.此处的parseConfiguration就是Configuration解析生成的极其重要的函数 this.parseConfiguration(this.parser.evalNode("/configuration"));其中的this.parser.evalNode("/configuration")是调用了XPathParser类的evalNode方法返回的是XNode对象。XNode对象保存了解析后的对应节点下的所有信息

private void parseConfiguration(XNode root) {
        try {
        //解析properties节点并设置到Configuration
            this.propertiesElement(root.evalNode("properties"));
       //解析typeAliases节点并设置到Configuration
            this.typeAliasesElement(root.evalNode("typeAliases"));
      //解析plugin节点并设置到Configuration
            this.pluginElement(root.evalNode("plugins"));
      //解析objectFactory节点并设置到Configuration
            this.objectFactoryElement(root.evalNode("objectFactory"));
         //解析objectWrapperFactory节点并设置到Configuration   this.objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));
//解析settings节点并设置到Configuration  
            this.settingsElement(root.evalNode("settings"));
//解析environments节点并设置到Configuration  即Environments对象
            this.environmentsElement(root.evalNode("environments"));
       //解析databaseIdProvider节点并设置到Configuration this.databaseIdProviderElement(root.evalNode("databaseIdProvider"));
   //解析typeHandlers节点并设置到Configuration    
            this.typeHandlerElement(root.evalNode("typeHandlers"));
 //解析mappers节点并设置到Configuration
            this.mapperElement(root.evalNode("mappers"));
        } catch (Exception var3) {
            throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + var3, var3);
        }
    }

5.XMLConfigBuilder的parseConfiguration里负责所有的配置文件节点的解析
总结:SqlSessionFactoryBuilder调用build方法,为了创建SqlSessionFactory。而SqlSessionFactory需要Configuration配置中心提供创建的条件。XMLConfigBuilder 将xml文件流进行初始化并parse返回Configuration,返回之前需要通过parseConfiguration来真正为Configuration设置信息,XPathParser负责将节点下的信息转换成XNode对象方便访问。

你可能感兴趣的:(MyBatis(一) xml文件解析流程 Configuration)