首页 > 代码库 > spring 启动流程
spring 启动流程
AbstractApplicationContext 分析
启动流程
// 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();
// Tell the subclass to refresh the internal bean factory.
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
// Prepare the bean factory for use in this context.
//预处理beanFactory
prepareBeanFactory(beanFactory);
try {
// Allows post-processing of the bean factory in context subclasses.
//预处理beanFactory之后 后期处理 目前也看到只有WEB实现(注册相关依赖)
postProcessBeanFactory(beanFactory);
// 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
spring 启动流程