轉(zhuǎn)帖|行業(yè)資訊|編輯:郝浩|2016-02-23 10:42:47.000|閱讀 583 次
概述:Ioc容器實(shí)際上就是Context組件結(jié)合其他兩個(gè)組件共同構(gòu)建了一個(gè)Bean關(guān)系網(wǎng),本篇講解了如何構(gòu)建這個(gè)關(guān)系網(wǎng)。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
Ioc容器實(shí)際上就是Context組件結(jié)合其他兩個(gè)組件共同構(gòu)建了一個(gè)Bean關(guān)系網(wǎng),如何構(gòu)建這個(gè)關(guān)系網(wǎng)?構(gòu)建的入口就在AbstractApplicationContext類的refresh方法中。這個(gè)方法的代碼如下:
public void refresh() throws BeansException, IllegalStateException { synchronized (this.startupShutdownMonitor) { // Prepare this context for refreshing. prepareRefresh(); // Tell the subclass to refresh the internal bean factory. ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory(); // Prepare the bean factory for use in this context. prepareBeanFactory(beanFactory); try { // Allows post- processing of the bean factory in context subclasses. postProcessBeanFactory(beanFactory); // Invoke factory processors registered as beans in& nbsp;the context. invokeBeanFactoryPostProcessors(beanFactory); // Register bean processors that intercept bean crea tion. registerBeanPostProcessors (beanFactory); // Initialize message source for this context. initMessageSource(); // Initialize event multicaster for this context. initApplicationEventMulticaster(); // Initialize other special beans in specific contex t subclasses. onRefresh(); // Check for listener beans and register them. registerListeners(); // Instantiate all remaining (non-lazy-init) singletons. finishBeanFactoryInitialization (beanFactory); // Last step: publish corresponding event. finishRefresh(); } catch (BeansException ex) { // Destroy already created singletons to avoid dangl ing resources. destroyBeans(); // Reset 'active' flag. cancelRefresh(ex); // Propagate exception to caller. throw ex; } } }
這個(gè)方法就是構(gòu)建整個(gè)Ioc容器過程的完整的代碼,了解了里面的每一行代碼基本上就了解大部分Spring的原理和功能了。
這段代碼主要包含這樣幾個(gè)步驟:
下面就結(jié)合代碼分析這幾個(gè)過程。
第二三句就是在創(chuàng)建和配置BeanFactory。這里是refresh也就是刷新配置,前面介紹了Context有可更新的子類,這里正是實(shí)現(xiàn)這個(gè)功能,當(dāng)BeanFactory已存在是就更新,如果沒有就新創(chuàng)建。
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn