Spring源码情操陶冶-AbstractApplicationContext

前言-阅读源码有利于陶冶情操,本文承接前文Spring源码情操陶冶-ContextLoader
约束:本文指定contextClass为默认的XmlWebApplicationContext

AbstractApplicationContext#refresh()

简单看下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 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;
			}
		}
	}

针对上述的代码逻辑,为了避免代码的凌乱以及放在同篇文章文字过长,遂作如下链接导航

  • AbstractApplicationContext#prepareRefresh
    刷新准备工作,@prepareRefresh

  • AbstractApplicationContext#obtainFreshBeanFactory
    涉及解析spring配置文件并封装为BeanDefinition对象保存至beanFactory中,@obtainFreshBeanFactory

  • AbstractApplicationContext#prepareBeanFactory
    beanFactory的准备工作,设置context的属性配置,@prepareBeanFactory

  • AbstractApplicationContext#postProcessBeanFactory
    主要添加ServletContextAwareProcessor处理类,@postProcessBeanFactory

  • AbstractApplicationContext#invokeBeanFactoryPostProcessors
    执行BeanDefinitionRegistryPostProcessors/BeanFactoryPostProcessors相关beans,@invokeBeanFactoryPostProcessors

  • AbstractApplicationContext#registerBeanPostProcessors
    注册所有实现BeanPostProcessor的接口bean到beanFactory的内部属性beanPostProcessors集合中,@registerBeanPostProcessors

  • AbstractApplicationContext#initMessageSource
    初始化资源配置,@initMessageSource

  • AbstractApplicationContext#initApplicationEventMulticaster
    初始化ApplictionEventMulticaster广播事件类,@initApplicationEventMulticaster

  • AbstractApplicationContext#onRefresh
    初始化themeSource,@onRefresh

  • AbstractApplicationContext#registerListeners
    注册ApplicationListener beans到ApplictionEventMulticaster广播集合,@registerListeners

  • AbstractApplicationContext#finishBeanFactoryInitialization
    实例化所有的非lazy-init类型的beans,@finishBeanFactoryInitialization

  • AbstractApplicationContext#finishRefresh
    完成刷新,并执行ContextRefreshedEvent事件,该事件涉及spring mvc,@finishRefresh

结束语

阅读源码的目的是为了了解其运行的机制,但可能涵盖的内容过广,可以对有兴趣的地方进行深刻的源码追踪,并理解其中的代码设计,不为是一件快乐的事情

posted @ 2017-04-12 16:55  南柯问天  阅读(1052)  评论(1编辑  收藏  举报