前面講了 簡單的實現方式通過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(); }
refresh()方法在 AbstractApplicationContext中,是個抽象類。這個方法運用了模板方法模式。
這裏面列出了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的實現類。在項目當中,我們經常會用到。