Spring源码分析-启动流程浅析
Spring源码分析-BeanDefinition
Spring源码分析-Bean管理查找与注册(1)
Spring源码分析-Bean管理查找与注册(2)
Spring源码分析-Bean管理循环依赖和三级缓存
Spring源码分析-Bean生命周期概述
Spring源码分析-Bean生命周期createBean
文章目录
Spring源码分析系列前言一、bean创建流程二、doCreateBean三、总结
前言
bean的生命周期状态虽然比较多,但是通过梳理代码其实也不是那么复杂。再次回顾bean的生命周期:
实例化 -->属性赋值–> bean后置处理器的before方法 --> 属性后置处理afterPropertiesSet --> bean初始化过程 --> bean后置处理器的after方法 --> 对象应用 --> bean销毁过程 --> gc回收
一、bean创建流程
通过流程图来,bean的创建流程也不太复杂啊
protected Object doCreateBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)throws BeanCreationException {// Instantiate the bean.BeanWrapper instanceWrapper = null;if (mbd.isSingleton()) {instanceWrapper = this.factoryBeanInstanceCache.remove(beanName);}if (instanceWrapper == null) {instanceWrapper = createBeanInstance(beanName, mbd, args); // 创建bean实例}Object bean = instanceWrapper.getWrappedInstance();Class<?> beanType = instanceWrapper.getWrappedClass();if (beanType != NullBean.class) {mbd.resolvedTargetType = beanType;}// Allow post-processors to modify the merged bean definition.synchronized (mbd.postProcessingLock) {if (!mbd.postProcessed) {try {applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);}catch (Throwable ex) {throw new BeanCreationException(mbd.getResourceDescription(), beanName,"Post-processing of merged bean definition failed", ex);}mbd.postProcessed = true;}}// Eagerly cache singletons to be able to resolve circular references// even when triggered by lifecycle interfaces like BeanFactoryAware.boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences &&isSingletonCurrentlyInCreation(beanName));if (earlySingletonExposure) {if (logger.isTraceEnabled()) {logger.trace("Eagerly caching bean '" + beanName +"' to allow for resolving potential circular references");}//先放到 三级缓存 中 这个地方是lambda表达//三级缓存addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean));//Object targetBean = getEarlyBeanReference(beanName, mbd, bean);//earlySingletonObjects.put(beanName, targetBean); //添加二级缓存//registeredSingletons.add(beanName);}// Initialize the bean instance.Object exposedObject = bean;try {//属性赋值populateBean(beanName, mbd, instanceWrapper);//执行后置处理器BeanPostProcessor, 如果动态代理场景 则返回的是动态代理对象exposedObject = initializeBean(beanName, exposedObject, mbd);}catch (Throwable ex) {if (ex instanceof BeanCreationException && beanName.equals(((BeanCreationException) ex).getBeanName())) {throw (BeanCreationException) ex;}else {throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Initialization of bean failed", ex);}}if (earlySingletonExposure) {// 这里是false,只查询一级缓存,二级缓存,不查三级缓存Object earlySingletonReference = getSingleton(beanName, false);if (earlySingletonReference != null) {if (exposedObject == bean) {exposedObject = earlySingletonReference;}else if (!this.allowRawInjectionDespiteWrapping && hasDependentBean(beanName)) {String[] dependentBeans = getDependentBeans(beanName);Set
三、总结
主要是流程图,代码逻辑不复杂,这里不再介绍,大家可按照流程图翻看源码。