例子java
同一個命名空間,可是有2個配置文件,A配置文件依賴於B配置文件信息,可是在加載時,先加載B配置文件的話,那麼就會報錯,前面章節已經捕獲了此異常並把相應的配置添加到Configuration(全局配置類)中,這裏就是在加載A配置文件後,再次加載B配置文件,也就把B配置信息添加到Configuration(全局配置類)裏,下面就來看一下代碼吧。apache
1 方法parsePendingResultMapssession
private void parsePendingResultMaps() { Collection<ResultMapResolver> incompleteResultMaps = configuration.getIncompleteResultMaps(); synchronized (incompleteResultMaps) { Iterator<ResultMapResolver> iter = incompleteResultMaps.iterator(); while (iter.hasNext()) { try { iter.next().resolve(); iter.remove(); } catch (IncompleteElementException e) { // ResultMap is still missing a resource... } } } }
2 方法parsePendingChacheRefsui
private void parsePendingChacheRefs() { Collection<CacheRefResolver> incompleteCacheRefs = configuration.getIncompleteCacheRefs(); synchronized (incompleteCacheRefs) { Iterator<CacheRefResolver> iter = incompleteCacheRefs.iterator(); while (iter.hasNext()) { try { iter.next().resolveCacheRef(); iter.remove(); } catch (IncompleteElementException e) { // Cache ref is still missing a resource... } } } }
3 方法parsePendingStatements
spa
private void parsePendingStatements() { Collection<XMLStatementBuilder> incompleteStatements = configuration.getIncompleteStatements(); synchronized (incompleteStatements) { Iterator<XMLStatementBuilder> iter = incompleteStatements.iterator(); while (iter.hasNext()) { try { iter.next().parseStatementNode(); iter.remove(); } catch (IncompleteElementException e) { // Statement is still missing a resource... } } } }
以上3個方法就是由於加載順序錯誤的重複處理邏輯,加載的代碼與前面同樣,這裏再也不分析。
code
總結ci
以上文章就是MyBatis對配置文件的初始化,這裏至少分析了60%,對理解MyBatis已經夠了。MyBatis也就剩最後一步了,創建SqlSessionFactory工廠,這個工廠也是惟一的,MyBatis默認使用的是org.apache.ibatis.session.defaults.DefaultSqlSessionFactory類。這裏之後在分析。rem