SpringIOC源码学习总结

入口方法:AnnotationConfigApplicationContext app = new AnnotationConfigApplicationContext(MyConfig.class);
初始化容器后,关注refresh()方法,该方法是synchronized修饰,所以之后都是安全的。 之后关注registerBeanPostProcessors();finishBeanFactoryInitialization(beanFactory);,前者实例化beanPostProcessors,后者实例化非lazy的singletonBan.

finishBeanFactoryInitialization(beanFactory)

重点关注beanFactory.getBean();
->createBean();->doCreateBean的时候会依次执行createBeanInstance();populateBean();initializeBean,分别是创建实例注入属性执行初始化方法

initializeBean()会先响应目标bean实现的aware接口->invokeAwareMethods.
接着applyBeanPostProcessorsBeforeInitialization以及invokeInitMethods(该方法包含initializeBean的afterPropertiesSet调用以及bean指定的initMethod方法)
然后applyBeanPostProcessorsAfterInitialization(包含得到代理对象等)
之后再设置disposable方法。
最后通过addSingleton(beanName, singletonObject);将其加入缓存。

看图比较直接:
在这里插入图片描述
ps: Spring 在实现其bean生命周期的同时,还实现了JSR_250定义的@PostConstruct、@PreDestory、@Resource, 其中 @PostConstruct执行实际是在afterPropertiesSet与initMehotd之前。@PreDestory 在 DisposableBean的destroy之前。

posted @ 2019-01-03 14:49  thewindkee  阅读(80)  评论(0编辑  收藏  举报