作者:zuoxiaolong8810(左瀟龍),轉載請注明出處,特別說明:本博文來自博主原博客,為保證新博客中博文的完整性,特復制到此留存,如需轉載請注明新博客地址即可。
最近由於工作和生活,學習耽擱了幾天,今天我們繼續接着上一章,分析FileSystemXmlApplicationContext的構造函數,到底都做了什么,導致IOC容器初始化成功。
public FileSystemXmlApplicationContext(String[] configLocations, boolean refresh, ApplicationContext parent) throws BeansException { super(parent); setConfigLocations(configLocations); if (refresh) { refresh(); } }
我們跟蹤上一章FileSystemXmlApplicationContext的構造函數,可以發現它最終調用的是上面這個形式重載的構造函數,其中的refresh方法,便是IOC容器初始化的入口。下面我們繼續跟蹤代碼進去看一下refresh方法。refresh方法位於AbstractApplicationContext中,這是一個抽象類,初步實現了ApplicationContext的一般功能,並且這里使用了模板模式,給以后要實現的子類提供了統一的模板。
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 the context. invokeBeanFactoryPostProcessors(beanFactory); // Register bean processors that intercept bean creation. registerBeanPostProcessors(beanFactory); // Initialize message source for this context. initMessageSource(); // Initialize event multicaster for this context. initApplicationEventMulticaster(); // Initialize other special beans in specific context 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 dangling resources. destroyBeans(); // Reset 'active' flag. cancelRefresh(ex); // Propagate exception to caller. throw ex; } } }
這里面列出了IOC容器初始化的大致步驟,第一步很容易看出來是初始化准備,這個方法里只是設置了一個活動標識,我們主要來看第二步,obtainFreshBeanFactory這個方法,它是用來告訴子類刷新內部的bean工廠,接下來我們跟蹤進去看看。
protected ConfigurableListableBeanFactory obtainFreshBeanFactory() { refreshBeanFactory(); ConfigurableListableBeanFactory beanFactory = getBeanFactory(); if (logger.isDebugEnabled()) { logger.debug("Bean factory for " + getDisplayName() + ": " + beanFactory); } return beanFactory; }
該方法中第一句便調用了另外一個refreshBeanFactory方法,這個方法是AbstractApplicationContext中的抽象方法,具體的實現並沒有在這個抽象類中實現,而是留給了子類,我們追蹤到這個子類當中去看一下。該方法又子類AbstractRefreshableApplicationContext實現,我們來看
protected final void refreshBeanFactory() throws BeansException { if (hasBeanFactory()) { destroyBeans(); closeBeanFactory(); } try { DefaultListableBeanFactory beanFactory = createBeanFactory(); beanFactory.setSerializationId(getId()); customizeBeanFactory(beanFactory); loadBeanDefinitions(beanFactory); synchronized (this.beanFactoryMonitor) { this.beanFactory = beanFactory; } } catch (IOException ex) { throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex); } }
方法加上了final關鍵字,也就是說此方法不可被重寫,可以很清楚的看到,IOC容器的初始化就是在這個方法里發生的,第一步先是判斷有無現有的工廠,有的話便會將其摧毀,否則,就會創建一個默認的bean工廠,也就是前面提到的DefaultListableBeanFactory,注意看loadBeanDefinitions(beanFactory);這里,當我們創建了一個默認的bean工廠以后,便是載入bean的定義。這與我們上一章所使用的原始的創建bean工廠的方式極為相似。
看到這里,其實不難看出,FileSystemXmlApplicationContext的初始化方法中,其實已經包含了我們上一章當中原始的創建過程,這個類是一個現有的,spring已經為我們實現好的BeanFactory的實現類。在項目當中,我們經常會用到。
今天天色已晚,改天鄙人帶着各位去看一下IOC容器建立的要義,也就是載入bean定義的實現。