学习源码之前先思考一个问题,我们为什么要学习源码?有什么用?
工作这么多年我总结了以下几点原因
我在学习Spring源码的过程中略有心得,通过流程图记录下源码执行流程、各种扩展点以及一些自己的理解。

源码的学习,都是从上面的流程学起,面试经常问题的Spring Bean的生命周期,就是上面图中,Bean创建的流程,在BeanFactory中有对应的注释,如下:

在源码中 AbstractApplicationContext #refresh 方法即为Spring Bean完整生命周期的体现
java public void refresh() throws BeansException, IllegalStateException { synchronized (this.startupShutdownMonitor) { StartupStep contextRefresh = this.applicationStartup.start("spring.context.refresh"); // 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); StartupStep beanPostProcess = this.applicationStartup.start("spring.context.beans.post-process"); // Invoke factory processors registered as beans in the context. invokeBeanFactoryPostProcessors(beanFactory); // Register bean processors that intercept bean creation. registerBeanPostProcessors(beanFactory); beanPostProcess.end(); // 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) { 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(); contextRefresh.end(); } } }
想要掌握Spring源码应该先掌握Spring的生命周期(Bean信息是怎么加载、解析、实例化、初始化、使用、销毁的),脑海中有一个框架轮廓,知道Spring先做了什么,后做了什么,有一个大概了解。
脑海中有了Spring的轮廓之后,开始思考细节,例如:Bean信息是怎么加载的?加载的Bean信息是直接生成可用的Bean吗?每一步流程都动脑思考扩展点,怎么应用到业务中,这个扩展适合哪些业务场景等。
在看源码的过程中,要学会关联思考,源码中往往是初始化数据后,n个方法后会用到的。
最后一点,学过源码后一定要思考、做笔记,回头来看学过的知识点能在脑海中立马回忆起来,争取做到对源码为所欲为的境界!!!
本文作者:张豪
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!