spring ApplicationContext中Bean的生命周期
阿新 • • 發佈:2019-01-15
rabl tco finall 註冊 反射 定義 temp nfa eset
AbstractApplicationContext
Spring的AbstractApplicationContext是ApplicationContext的抽象實現類,該抽象類的refresh方法定義了spring容器在加載配置文件後的各項處理過程
public void refresh() throws BeansException, IllegalStateException { synchronized (this.startupShutdownMonitor) { prepareRefresh(); // 初始化BeanFactoryAbstractApplicationContext源碼ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory(); prepareBeanFactory(beanFactory); try { postProcessBeanFactory(beanFactory); /** * 調用工廠後處理器: * 根據反射機制找出所有實現了BeanFactoryPostProcessor接口的Bean, * 並調用其postProcessBeanFactory()接口方法*/ invokeBeanFactoryPostProcessors(beanFactory); /** * 註冊Bean後處理器: * 根據反射機制找出所有實現了BeanPostProcessor接口的Bean, * 並註冊 */ registerBeanPostProcessors(beanFactory); // 初始化消息源:初始化容器的國際化消息資源initMessageSource(); // 初始化Application事件廣播器 initApplicationEventMulticaster(); // 初始化其他特殊的Bean,由具體子類實現 onRefresh(); // 註冊事件監聽器 registerListeners(); // 初始化所有單例的Bean,使用懶加載的除外 finishBeanFactoryInitialization(beanFactory); // Last step: publish corresponding event. finishRefresh(); } catch (BeansException ex) { if (logger.isWarnEnabled()) { logger.warn("Exception encountered during context initialization - " + "cancelling refresh attempt: " + ex); } // Destroy already created singletons to avoid dangling resources. destroyBeans(); // Reset ‘active‘ flag. cancelRefresh(ex); // Propagate exception to caller. throw ex; } finally { // Reset common introspection caches in Spring‘s core, since we // might not ever need metadata for singleton beans anymore... resetCommonCaches(); } } }
spring ApplicationContext中Bean的生命周期