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的構造方法,如下:
1 public XMLConfigBuilder(Reader reader) { 2 this(reader, null, null); 3 } 4 5 public XMLConfigBuilder(Reader reader, String environment) { 6 this(reader, environment, null); 7 } 8 9 public XMLConfigBuilder(Reader reader, String environment, Properties props) { 10 this(new XPathParser(reader, true, props, new XMLMapperEntityResolver()), environment, props); 11 } 12 13 public XMLConfigBuilder(InputStream inputStream) { 14 this(inputStream, null, null); 15 } 16 17 public XMLConfigBuilder(InputStream inputStream, String environment) { 18 this(inputStream, environment, null); 19 } 20 21 public XMLConfigBuilder(InputStream inputStream, String environment, Properties props) { 22 this(new XPathParser(inputStream, true, props, new XMLMapperEntityResolver()), environment, props); 23 } 24 25 private XMLConfigBuilder(XPathParser parser, String environment, Properties props) { 26 super(new Configuration());//調用父類的構造方法 27 ErrorContext.instance().resource("SQL Mapper Configuration"); 28 this.configuration.setVariables(props); 29 this.parsed = false; 30 this.environment = environment; 31 this.parser = parser; 32 }
很顯然6個public的構造方法都是根據mybatis的配置文件流創建一個XPathParser對象,然后最終都調用了私有的構造方法,而私有的構造方法先是調用了父類BaseBuilder的構造方法,然后分別根據參數給四個屬性賦值。
而上一篇文章提到了SqlSessionFactoryBuilder中是通過創建一個XMLConfigBuilder對象,然后調用了對象的parse()方法獲取到一個Configuration對象。接下來就先看看XMLConfigBuilder的parse方法,如下:、
1 public Configuration parse() { 2 if (parsed) {//判斷Configuration是否解析過,Configuration是全局變量,只需要解析創建一次即可 3 throw new BuilderException("Each XMLConfigBuilder can only be used once."); 4 } 5 parsed = true; 6 parseConfiguration(parser.evalNode("/configuration"));//調用下面的方法,parser.evalNode("/configuration")解析XML配置的configuration節點的內容,得到XNode對象 7 return configuration; 8 } 9 //根據root中存儲的是configuration節點的內容 10 private void parseConfiguration(XNode root) { 11 try { 12 Properties settings = settingsAsPropertiess(root.evalNode("settings"));//設置settings配置 13 //issue #117 read properties first 14 propertiesElement(root.evalNode("properties"));//設置properties配置 15 loadCustomVfs(settings); 16 typeAliasesElement(root.evalNode("typeAliases"));//設置typeAliases配置 17 pluginElement(root.evalNode("plugins"));//設置plugins配置 18 objectFactoryElement(root.evalNode("objectFactory"));//設置objectFactory配置 19 objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));//設置objectWrapperFactory配置 20 reflectorFactoryElement(root.evalNode("reflectorFactory"));//設置reflectFactory配置 21 settingsElement(settings); 22 // read it after objectFactory and objectWrapperFactory issue #631 23 environmentsElement(root.evalNode("environments"));//設置environments配置 24 databaseIdProviderElement(root.evalNode("databaseIdProvider"));//設置databaseIdProvider配置 25 typeHandlerElement(root.evalNode("typeHandlers"));//設置typeHandlers配置 26 mapperElement(root.evalNode("mappers"));//設置mappers配置 27 } catch (Exception e) { 28 throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e); 29 } 30 }
可以看出parse的作用是解析mybatis-config.xml的configuration節點的內容,然后挨個賦值給configuration對象的屬性;
而XMLConfigBuilder的其他私有方法都是給根據XNode對象(XML配置的configuration節點內容)來給全局配置變量configuration的屬性進行賦值,關於Configuration類的解析下一章會解析
總結:XMLConfigBuilder類的作用是根據全局配置文件mybatis-config.xml的流文件進行解析,解析xml中的各個節點,然后創建一個Configuration對象,並將xml中的節點屬性賦值給Configuration對象