第二章 IoC Bean的初始化與延遲加載
在默認情況下,Spring的ApplicationContext容器在啟動的時候,會自動實例化所有singleton的Bean並緩存在容器中。雖然啟動時會花費一些時間,但帶來兩個好處:
1、對Bean提前實例化操作會及早發現一些潛在的配置問題;
2、Bean以緩存的方式保存,當運行期使用到該Bean的時候無需再實例化,加快運行的效率。
如果用戶不希望容器啟動的時候提前實例化singleton的Bean,可以通過lazy-init屬性進行控製:
<bean scope="singleton" lazy-init="true"></bean>
也可以在beans標簽中使用default-lazy-init對所有的singleton的Bean進行延遲加載:
<beans xmlns="https://www.springframework.org/schema/beans" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns:p="https://www.springframework.org/schema/p" xsi:schemaLocation="https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-3.0.xsd" default-lazy-init="true">
被設置為延遲加載的Bean在某些情況下依舊會提前實例化:如果這個Bean被其它需要提前實例化的Bean引用到,那麼Spring也將忽略延遲加載設置。
一般我們不做延遲加載。
最後更新:2017-04-03 18:52:13
上一篇:
android源碼地址
下一篇:
C++內存管理學習筆記(5)
軟件更新時候出現和原包名衝突
銀泰首個新零售雙11:CEO陳曉東“微頓悟”了
編程思維訓練(一)
金牌導購係統開發平台搭建
iBATIS多表查詢之N+1 Select
Apache HttpComponents Client 4.0快速入門/升級-1.GET方法訪問網頁
ARM免預付授權費用計劃增加Cortex-M3架構設計 加速物聯網發展
工作五年“攢”夠100萬,程序猿們,我可沒開玩笑!8條建議抱走不謝
錯誤整理:No plugin found for prefix 'jetty' in the current project and in the plugin groups
The specified executable is not a validapplication for this OS platform.