spring 啓動流程

AbstractApplicationContext 分析web

啓動流程
// Prepare this context for refreshing.
prepareRefresh();
1.
// Initialize any placeholder property sources in the context environment
//預處理配置,目前看到WEB層實現(預處理WEB配置)
initPropertySources();

2. // Validate that all properties marked as required are resolvable
// see ConfigurablePropertyResolver#setRequiredProperties
//驗證系統配置
getEnvironment().validateRequiredProperties();spring

// Tell the subclass to refresh the internal bean factory.
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();post

// Prepare the bean factory for use in this context.
//預處理beanFactory
prepareBeanFactory(beanFactory);ui


try {
// Allows post-processing of the bean factory in context subclasses.
//預處理beanFactory以後 後期處理 目前也看到只有WEB實現(註冊相關依賴)
postProcessBeanFactory(beanFactory);this

// Invoke factory processors registered as beans in the context.
//執行beanFactory後期處理
invokeBeanFactoryPostProcessors(beanFactory);
//處理流程優先級(自己實例不考慮)
1.BeanDefinitionRegistryPostProcessor
2.BeanFactoryPostProcessor
3.BeanFactoryPostProcessor(插件處理) priorityOrdered -> ordered ->nonOrdered

// Register bean processors that intercept bean creation.
//註冊後期處理器 (掃描接口 BeanPostProcessor)
registerBeanPostProcessors(beanFactory);
//執行優先級 PriorityOrdered -> ordered -> nonOrdered插件

// Initialize message source for this context.
//初始化 國際化對象
initMessageSource();線程

// Initialize event multicaster for this context.
//初始化事件廣播對象
initApplicationEventMulticaster();對象

// Initialize other special beans in specific context subclasses.
//完成處理後期後,刷新 目前也只看到web跟 DefaultLifecycleProcessor 實現
onRefresh();接口

// Check for listener beans and register them.
//註冊處理spring 事件 優先級自己實例事件 -> 掃描全部 spring 事件接口對象
registerListeners();生命週期

// Instantiate all remaining (non-lazy-init) singletons.
//初始化完成 beanFactory 處理器
finishBeanFactoryInitialization(beanFactory);

// Last step: publish corresponding event.
//完成beanFactory容器處理
finishRefresh();
1.initLifecycleProcessor();//初始化生命週期處理器
2.getLifecycleProcessor().onRefresh();//刷新生命週期處理器
3.publishEvent(new ContextRefreshedEvent(this)); //發佈ContextRefreshedEvent事件
4.registerApplicationContext();//註冊上下文


關閉容器事件
1.Runtime.getRuntime().removeShutdownHook(this.shutdownHook); //移動JVM線程級監控
2.LiveBeansView.unregisterApplicationContext(this);//清空上下文 unregisterMBean
3.publishEvent(new ContextClosedEvent(this)); //發佈關閉事件
4.getLifecycleProcessor().onClose();
5.destroyBeans()//沒看到有實現
6.closeBeanFactory();//刪除BEAN
7.// Let subclasses do some final clean-up if they wish...
onClose()


小結 :
1.預處理後注入相關依賴 實現 BeanFactoryPostProcessor
2.預處理後修改bean 實現 BeanPostProcessor
3.完成預處理後執行 實現 DefaultLifecycleProcessor

4.完成整個加載後執行 實現 ApplicationListener ContextRefreshedEvent 事件

5.關閉而且在銷燬以前 實現 DefaultLifecycleProcessor 或 ApplicationListener ContextClosedEvent 事件

FactoryBean,AbstractFactoryBean<T> ,InitializingBean ,DisposableBean,BeanFactoryAware

相關文章
相關標籤/搜索