延时加载refresh()方法
生活随笔
收集整理的這篇文章主要介紹了
延时加载refresh()方法
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
先從IOC 容器的初始化過(guò)程開(kāi)始,我們知道IOC 容器讀入已經(jīng)定位的Bean 定義資源是從refresh()方法開(kāi)始的,我們首先從AbstractApplicationContext 類(lèi)的refresh()方法入手分析,源碼如下:
@Override public void refresh() throws BeansException, IllegalStateException {synchronized (this.startupShutdownMonitor) {// Prepare this context for refreshing.//1、調(diào)用容器準(zhǔn)備刷新的方法,獲取容器的當(dāng)時(shí)時(shí)間,同時(shí)給容器設(shè)置同步標(biāo)識(shí)prepareRefresh();// Tell the subclass to refresh the internal bean factory.//2、告訴子類(lèi)啟動(dòng)refreshBeanFactory()方法,Bean定義資源文件的載入從//子類(lèi)的refreshBeanFactory()方法啟動(dòng)ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();// Prepare the bean factory for use in this context.//3、為BeanFactory配置容器特性,例如類(lèi)加載器、事件處理器等prepareBeanFactory(beanFactory);try {// Allows post-processing of the bean factory in context subclasses.//4、為容器的某些子類(lèi)指定特殊的BeanPost事件處理器postProcessBeanFactory(beanFactory);// Invoke factory processors registered as beans in the context.//5、調(diào)用所有注冊(cè)的BeanFactoryPostProcessor的BeaninvokeBeanFactoryPostProcessors(beanFactory);// Register bean processors that intercept bean creation.//6、為BeanFactory注冊(cè)BeanPost事件處理器.//BeanPostProcessor是Bean后置處理器,用于監(jiān)聽(tīng)容器觸發(fā)的事件registerBeanPostProcessors(beanFactory);// Initialize message source for this context.//7、初始化信息源,和國(guó)際化相關(guān).initMessageSource();// Initialize event multicaster for this context.//8、初始化容器事件傳播器.initApplicationEventMulticaster();// Initialize other special beans in specific context subclasses.//9、調(diào)用子類(lèi)的某些特殊Bean初始化方法onRefresh();// Check for listener beans and register them.//10、為事件傳播器注冊(cè)事件監(jiān)聽(tīng)器.registerListeners();// Instantiate all remaining (non-lazy-init) singletons.//11、初始化所有剩余的單例BeanfinishBeanFactoryInitialization(beanFactory);// Last step: publish corresponding event.//12、初始化容器的生命周期事件處理器,并發(fā)布容器的生命周期事件finishRefresh();}catch (BeansException ex) {if (logger.isWarnEnabled()) {logger.warn("Exception encountered during context initialization - " +"cancelling refresh attempt: " + ex);}// Destroy already created singletons to avoid dangling resources.//13、銷(xiāo)毀已創(chuàng)建的BeandestroyBeans();// Reset 'active' flag.//14、取消refresh操作,重置容器的同步標(biāo)識(shí)。cancelRefresh(ex);// Propagate exception to caller.throw ex;}finally {// Reset common introspection caches in Spring's core, since we// might not ever need metadata for singleton beans anymore...//15、重設(shè)公共緩存resetCommonCaches();}} }在refresh()方法中ConfigurableListableBeanFactorybeanFactory = obtainFreshBeanFactory();啟動(dòng)了Bean 定義資源的載入、注冊(cè)過(guò)程,而finishBeanFactoryInitialization 方法是對(duì)注冊(cè)后的Bean定義中的預(yù)實(shí)例化(lazy-init=false,Spring 默認(rèn)就是預(yù)實(shí)例化,即為true)的Bean 進(jìn)行處理的地方。
?
?
總結(jié)
以上是生活随笔為你收集整理的延时加载refresh()方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 关于延时加载
- 下一篇: finishBeanFactoryIni