1. 程式人生 > >Spring Ioc(4)——web中BeanDefinition的載入

Spring Ioc(4)——web中BeanDefinition的載入

我們繼續上一節,其實spring Ioc的實現從巨集觀上看,分為三步: (1)BeanDefinition的載入 (2)BeanDefinition的解析 (3)BeanDefinition的註冊 1、BeanDefinition載入的入口 那麼分別一個一個來分析,先從載入入手。BeanDefinition的載入,就相當於把我們定義的BeanDefinition在IOC容器中表示成spring內部的資料結構的過程,BeanDefinition在Spring容器中是用HashMap來維護的。我們同樣的從入口著手,看其實如何實現載入的。 上一節我們談到了WebApplicationContext的構建,在其構建完畢後,便呼叫了AbstractApplicationContext的refresh方法,其實這也是BeanDefinition載入的入口。從圖中可以看到AbstractApplicationContext是XmlWebApplicationContext的基類。

refresh是很重要的一個方法,描述了整個初始化的過程,其中有一段程式碼通知子類去refresh BeanFactory,那麼我們來看看AbstractRefreshableApplicationContext的核心程式碼: protectedfinalvoid refreshBeanFactory() throws BeansException { if (hasBeanFactory()) {             destroyBeans();             closeBeanFactory();         } try {                         //構建一個DefaultListableBeanFactory
            DefaultListableBeanFactory beanFactory = createBeanFactory();             beanFactory.setSerializationId(getId());             customizeBeanFactory(beanFactory);                         //啟動beanDefinition的載入             loadBeanDefinitions(beanFactory); synchronized (this.beanFactoryMonitor
) {
this.beanFactory = beanFactory;             }         } catch (IOException ex) { thrownew ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);         }     } 程式碼中構建了一個DefaultListableBeanFactory,並且啟動了beanDefinition的載入,具體的載入實現是在XmlWebApplicationContext中實現的。 2、BeanDefinition載入的過程 我們從上面的原始碼就可以看到,在XmlWebApplicationContext中,通過方法loadBeanDefinition啟動了載入,實際上載入的過程是在XmlBeanDefinitionReader中完成的。 publicint loadBeanDefinitions(EncodedResource encodedResource) throws BeanDefinitionStoreException {         Assert.notNull(encodedResource, "EncodedResource must not be null"); if (logger.isInfoEnabled()) { logger.info("Loading XML bean definitions from " + encodedResource.getResource());         }         Set<EncodedResource> currentResources = this.resourcesCurrentlyBeingLoaded.get(); if (currentResources == null) {             currentResources = new HashSet<EncodedResource>(4); this.resourcesCurrentlyBeingLoaded.set(currentResources);         } if (!currentResources.add(encodedResource)) { thrownew BeanDefinitionStoreException( "Detected cyclic loading of " + encodedResource + " - check your import definitions!");         } try {                       //獲取xml檔案和io的InputSource             InputStream inputStream = encodedResource.getResource().getInputStream(); try {                 InputSource inputSource = new InputSource(inputStream); if (encodedResource.getEncoding() != null) {                     inputSource.setEncoding(encodedResource.getEncoding());                 }                              //啟動BeanDefiniton的載入 return doLoadBeanDefinitions(inputSource, encodedResource.getResource());             } finally {                 inputStream.close();             }         } catch (IOException ex) { thrownew BeanDefinitionStoreException( "IOException parsing XML document from " + encodedResource.getResource(), ex);         } finally {             currentResources.remove(encodedResource); if (currentResources.isEmpty()) { this.resourcesCurrentlyBeingLoaded.remove();             }         }     } 我們再來看呼叫的doLoadBeanDefinitions方法: protectedint doLoadBeanDefinitions(InputSource inputSource, Resource resource) throws BeanDefinitionStoreException { try { int validationMode = getValidationModeForResource(resource);             //獲取xml檔案的Document物件,完成載入BeanDefinition。             Document doc = this.documentLoader.loadDocument(                     inputSource, getEntityResolver(), this.errorHandler, validationMode, isNamespaceAware());                             //BeanDefinition的解析(後面繼續討論) return registerBeanDefinitions(doc, resource);         } catch (BeanDefinitionStoreException ex) { throw ex;         } catch (SAXParseException ex) { thrownew XmlBeanDefinitionStoreException(resource.getDescription(), "Line " + ex.getLineNumber() + " in XML document from " + resource + " is invalid", ex);         } catch (SAXException ex) { thrownew XmlBeanDefinitionStoreException(resource.getDescription(), "XML document from " + resource + " is invalid", ex);         } catch (ParserConfigurationException ex) { thrownew BeanDefinitionStoreException(resource.getDescription(), "Parser configuration exception parsing XML from " + resource, ex);         } catch (IOException ex) { thrownew BeanDefinitionStoreException(resource.getDescription(), "IOException parsing XML document from " + resource, ex);         } catch (Throwable ex) { thrownew BeanDefinitionStoreException(resource.getDescription(), "Unexpected exception parsing XML document from " + resource, ex);         }     }

相關推薦

Spring Ioc4——webBeanDefinition載入

我們繼續上一節,其實spring Ioc的實現從巨集觀上看,分為三步: (1)BeanDefinition的載入 (2)BeanDefinition的解析 (3)BeanDefinition的註冊 1、BeanDefinition載入的入口 那麼分別一個一個來分析

Spring學習4IOC容器配置bean:定義與實例化

dimp 工廠類 def 流程 行為 更多 多個 scrip 編譯報錯 一. IOC容器配置   1. 一些概念   (1)IOC容器:   定義:具有管理對象和管理對象之間的依賴關系的容器。   作用:應用程序無需自己創建對象,對象由IOC容器創建並組裝。BeanFac

FPGA小白學習之路4PLL的locked信號解析

count 功能 style use doc 分享圖片 tar cte cal ALTPLL中的areset,locked的使用   轉自:http://www.360doc.com/content/13/0509/20/9072830_284220258.shtml   

Spring bootWeb開發

final 一次 org init 調用 mat 重要 映射 ppi 上篇文章介紹了Spring boot初級教程:spring boot(一):入門篇,方便大家快速入門、了解實踐Spring boot特性;本篇文章接著上篇內容繼續為大家介紹spring boot的其它特性

python---RabbitMQ4exchange模糊匹配topic

art OS pan () clu message exc llb color 和關鍵字相似 生產者: # coding:utf8 # __author: Administrator # date: 2018/3/15 0015 # /usr/bin/env

Spring REST4

總結 pack XML 技術 method type con too request REST風格   /user/1 get請求   獲取用戶   /user/1  post請求  新增用戶   /user/1  put請求  更新用戶   /user

spring IoC autowiring(自動依賴裝配)的實現

emp 自動 bst () this on() 構造 clas span   在前面對IoC容器實現原理的分析中,一直是通過BeanDefinition的屬性值和構造函數以顯式的方式對Bean 的依賴關系進行管理的。在Spring中,相對這種顯式的依賴管理方式,IoC容器還

spring IoC Bean的依賴檢查

容器 第一次 ioc 特性 ble div name contain ted   在使用Spring的時候,如果應用設計比較復雜,那麽在這個應用中,IoC管理的Bean的個數可能非常多, 這些Bean之間的相互依賴關系也會非常復雜。在一般情況下,Bean的依賴註入是在應用第

4pysparkdataframe的清理操作

tab tin eight 存在 ast wid column idt src 1、交叉表(crosstab): pandas中也有,常和pivot_table比較。 查看家庭ID與評分的交叉表: 2、處理缺失值:fillna withColumn:新增一列數據 ca

spring學習4

    在spring容器內拼湊bean叫做裝配。裝配bean的時候,需要告訴容器哪些bean以及容器如何使用依賴注入將它們配合在一起。 上下文定義檔案的根元素是<beans>,<beans>中有很多<bean> id不能重複,class要寫全。

Spring基礎:快速入門spring boot4:使用slf4j輸出日誌

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!        

Spring基礎:快速入門spring cloud4:API閘道器之Zuul

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!        

Python學習筆記4——Matplotlib的annotate註解的用法

在上一篇博文ID3決策樹演算法中,繪製決策樹時,使用了Matplotlib的註解工具annotate,藉此機會系統學習一下annotate的用法。 annotate用於在圖形上給資料新增文字註解,而且

Web填坑之路4 --- web server大概解釋

Web Server中文名稱叫網頁伺服器或web伺服器。web伺服器也稱為WWW伺服器,主要功能是提供網上資訊瀏覽服務。 Web伺服器可以解析HTTP協議。當Web伺服器接收到一個HTTP請求,會返回一個HTTP響應,例如返回一個HTML頁面。為了處理一個請求,Web伺服器可以響應一個靜態頁面或圖片,進行頁

Spring框架入門之Spring IOCXML& Bean工廠

1、Spring框架入門之Spring IOC(XML)& Bean工廠 1.1 什麼是Spring框架?          Spring是一個開源框架,Spring是於2003年興起的一個輕量級的J

Spring IOC單例、非單例+容器關閉了,getBean獲取的物件小老弟你咋還在蹦躂? day--07

Spring IOC(一)單例、非單例+容器關閉了,getBean獲取的物件(小老弟)你咋還在蹦躂? day–07 一路綠燈學過去的就不記了,只記錄重點和實驗過程,另外內容順序排列可能引起不適,但是是根我碰到問題順序走的,,,在這向有一丁點可能看到這篇文章的您抱歉。 一、.Sprin

Spring aop4---aspectj

今天講一下Spring aop中的AspectJ: 一、AspectJ簡介 AspectJ是一個面向切面的框架,它擴充套件了Java語言。AspectJ定義了AOP語法,所以它有一個專門的編譯器 用來生成遵守Java字節編碼規範的Class檔案。 二、優勢: aspectj技術主

【初探Spring】——Spring IOC:初始化過程—Resource定位

我們知道Spring的IoC起到了一個容器的作用,其中裝得都是各種各樣的Bean。同時在我們剛剛開始學習Spring的時候都是通過xml檔案來定義Bean,Spring會某種方式載入這些xml檔案,然後根據這些資訊繫結整個系統的物件,最終組裝成一個可用的基於輕量級容器的應用系

用flask開發個人部落格4—— flask4種全域性變數

一  current_app         current_app代表當前的flask程式例項,使用時需要flask的程式上下文啟用,我們以本專欄第一篇文章中寫的test.py為例介紹下它的用法: 1.1 啟用程式上下文 >>> from test i

Spring Redis4物件Hash對映

Hash mapping 使用Redis的hash結構儲存物件有以下三種方式 1. 使用HashOperations和序列化(見上一節) 2. 使用Redis持久化(推薦,見後續章節) 3. 使