记录并分享一下本人学习spring源码的过程,有什么问题或者补充会持续更新。欢迎大家指正!
环境: spring5.X + idea
之前分析了Spring读取xml文件的所有信息封装成beanDefinition,并存到了beanDefinitionMap中以便我们后续使用。
传送门Spring源码分析01-(xml解析)
今天学习spring容器创建对象的整个过程,或者叫对象的生命周期
// 第一步是完成对象创建所需要的基本数据也就是 beanDefinition BeanFactory bf = new XmlBeanFactory(new ClassPathResource("applicationContext.xml")); // 然后开始创建对象 User user = bf.getBean("user"); // 1. getBean() 进入 AbstractBeanFactory 中的 doGetBean 方法去做创建对象的具体步骤。 protected <T> T doGetBean(String name, ....................... ..) throws BeansException { String beanName = this.transformedBeanName(name); // 先去 DefaultSingletonBeanRegistry 中的 singletonObjects 获取对应的bean, // 因为spring 默认 scope = singleton 单例bean不需要每次都创建 Object sharedInstance = this.getSingleton(beanName); Object bean; ........... // scope = Singleton if(mbd.isSingleton()) { sharedInstance = this.getSingleton(beanName, () -> { try { // 开始去创建bean return this.createBean(beanName, mbd, args); } ...... }); bean = this.getObjectForBeanInstance(sharedInstance, name, beanName, mbd); // scope = Prototype } else if(mbd.isPrototype()) { var11 = null; Object prototypeInstance; try { this.beforePrototypeCreation(beanName); // 开始去创建bean prototypeInstance = this.createBean(beanName, mbd, args); } ........... } else { // 其他情况 ... } } // this.createBean(.....) 进入到 AbstractAutowireCapableBeanFactory 中的 createBean方法 protected Object createBean(String beanName...........) throws BeanCreationException { try { beanInstance = this.doCreateBean(beanName, mbdToUse, args); if(this.logger.isDebugEnabled()) { this.logger.debug("Finished creating instance of bean '" + beanName + "'"); } return beanInstance; } .......... }
dodoCreateBean 方法就是整个创建对象的核心方法
protected Object doCreateBean(String beanName............) throws BeanCreationException { BeanWrapper instanceWrapper = null; if(mbd.isSingleton()) { instanceWrapper = (BeanWrapper)this.factoryBeanInstanceCache.remove(beanName); } if(instanceWrapper == null) { // 这里是 第一步创建对象 instanceWrapper = this.createBeanInstance(beanName, mbd, args); } }
// 还是刚才的 doCreateBean方法中 protected Object doCreateBean(String beanName............) throws BeanCreationException { BeanWrapper instanceWrapper = null; if(mbd.isSingleton()) { instanceWrapper = (BeanWrapper)this.factoryBeanInstanceCache.remove(beanName); } if(instanceWrapper == null) { // 这里是 第一步创建对象 instanceWrapper = this.createBeanInstance(beanName, mbd, args); } Object exposedObject = bean; try { // 这一步是填充属性 this.populateBean(beanName, mbd, instanceWrapper); exposedObject = this.initializeBean(beanName, exposedObject, mbd); } catch (Throwable var18) { if(var18 instanceof BeanCreationException && beanName.equals(((BeanCreationException)var18).getBeanName())) { throw (BeanCreationException)var18; } throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Initialization of bean failed", var18); } }
// 还是刚才的 doCreateBean方法中 protected Object doCreateBean(String beanName............) throws BeanCreationException { BeanWrapper instanceWrapper = null; if(mbd.isSingleton()) { instanceWrapper = (BeanWrapper)this.factoryBeanInstanceCache.remove(beanName); } if(instanceWrapper == null) { // 这里是 第一步创建对象 instanceWrapper = this.createBeanInstance(beanName, mbd, args); } Object exposedObject = bean; try { // 这一步是填充属性 this.populateBean(beanName, mbd, instanceWrapper); // 这一步是初始化对象 exposedObject = this.initializeBean(beanName, exposedObject, mbd); } catch (Throwable var18) { if(var18 instanceof BeanCreationException && beanName.equals(((BeanCreationException)var18).getBeanName())) { throw (BeanCreationException)var18; } throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Initialization of bean failed", var18); } }
DisposableBeanAdapter implements DisposableBean, Runnable, Serializable{ public void destroy() { //销毁 } }
感谢您的阅读,有什么意见和问题欢迎评论区留言!书写不易!
觉得文章对你有帮助记得给我点个赞,欢迎大家关注和转发文章!