轉帖|行業資訊|編輯:郝浩|2016-02-23 10:42:47.000|閱讀 583 次
概述:Ioc容器實際上就是Context組件結合其他兩個組件共同構建了一個Bean關系網,本篇講解了如何構建這個關系網。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Ioc容器實際上就是Context組件結合其他兩個組件共同構建了一個Bean關系網,如何構建這個關系網?構建的入口就在AbstractApplicationContext類的refresh方法中。這個方法的代碼如下:
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; } } }
這個方法就是構建整個Ioc容器過程的完整的代碼,了解了里面的每一行代碼基本上就了解大部分Spring的原理和功能了。
這段代碼主要包含這樣幾個步驟:
下面就結合代碼分析這幾個過程。
第二三句就是在創建和配置BeanFactory。這里是refresh也就是刷新配置,前面介紹了Context有可更新的子類,這里正是實現這個功能,當BeanFactory已存在是就更新,如果沒有就新創建。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn