容器的扩展功能主要实现为:
org.springframework.context.support.AbstractApplicationContext.refresh()
public void refresh() throws BeansException, IllegalStateException { synchronized (this.startupShutdownMonitor) { // 准备刷新的上下文环境 // Prepare this context for refreshing. prepareRefresh(); // 初始化BeanFactory,并进行xml文件读取 // Tell the subclass to refresh the internal bean factory. ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory(); // 对BeanFactory进行各种功能填充 // Prepare the bean factory for use in this context. prepareBeanFactory(beanFactory); try { // 子类覆盖方法做额外的处理 // Allows post-processing of the bean factory in context subclasses. postProcessBeanFactory(beanFactory); // 激活各种BeanFactory处理器 // Invoke factory processors registered as beans in the context. invokeBeanFactoryPostProcessors(beanFactory); // 注册拦截bean创建的bean处理器,这里只是注册,真正的调用时在getBean时候 // Register bean processors that intercept bean creation. registerBeanPostProcessors(beanFactory); // 为上下文初始化Message源,即不同语言的消息体,国际化处理 // Initialize message source for this context. initMessageSource(); // 初始化应用消息广播器,并放入ApplicationEventMulticaster中 // Initialize event multicaster for this context. initApplicationEventMulticaster(); // 留给子类来初始化其他的bean // Initialize other special beans in specific context subclasses. onRefresh(); // 在所有注册的bean中查找listener bean,注册到消息广播器中 // Check for listener beans and register them. registerListeners(); // 初始化剩下的单实例(非延迟加载的) // Instantiate all remaining (non-lazy-init) singletons. finishBeanFactoryInitialization(beanFactory); // 完成刷新过程,通知生命周期处理器lifecycleProcessor刷新过程,同时发出ContextRefreshEvent通知别人 // 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; } } }
参考:spring源码深度解析