在網上已經有跟多Bean的生命週期的博客,可是不少都是基於比較老的版本了,最近把整個流程畫成了一個流程圖。待會兒使用流程圖,說明以及代碼的形式來講明整個聲明週期的流程。注意由於代碼比較多,這裏的流程圖只畫出了大概的流程,具體的能夠深刻代碼緩存
這裏的流程圖的入口在AbstractBeanFactory類的doGetBean方法,這裏能夠配合前面的getBean方法分析文章進行閱讀。主要流程就是app
2.1 建立Bean以前函數
這個流程圖對應的代碼在AbstractAutowireCapableBeanFactory類的createBean方法中。post
(1)Bean的屬性中的beforeInstantiationResolved字段是否爲true,默認是false。學習
(2)Bean是原生的Beanui
(3)Bean的hasInstantiationAwareBeanPostProcessors屬性爲true,這個屬性在Spring準備刷新容器錢轉杯BeanPostProcessors的時候會設置,若是當前Bean實現了InstantiationAwareBeanPostProcessor則這個就會是true。this
當三個條件都存在的時候,就會調用實現的InstantiationAwareBeanPostProcessor接口的postProcessBeforeInstantiation方法,而後獲取返回的Bean,若是返回的Bean不是null還會調用實現的BeanPostProcessor接口的postProcessAfterInitialization方法,這裏用代碼說明spa
protected Object resolveBeforeInstantiation(String beanName, RootBeanDefinition mbd) { Object bean = null; //條件1 if (!Boolean.FALSE.equals(mbd.beforeInstantiationResolved)) { //條件2跟條件3 if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) { Class<?> targetType = determineTargetType(beanName, mbd); if (targetType != null) { //調用實現的postProcessBeforeInstantiation方法 bean = applyBeanPostProcessorsBeforeInstantiation(targetType, beanName); if (bean != null) { //調用實現的postProcessAfterInitialization方法 bean = applyBeanPostProcessorsAfterInitialization(bean, beanName); } } } //不知足2或者3的時候就會設置爲false mbd.beforeInstantiationResolved = (bean != null); } return bean; }
2.1 真正的建立Bean,doCreateBean3d
這個代碼的實現仍是在AbstractAutowireCapableBeanFactory方法中。流程是code
1.先檢查instanceWrapper變量是否是null,這裏通常是null,除非當前正在建立的Bean在factoryBeanInstanceCache中存在這個是保存還沒建立完成的FactoryBean的集合。
2.調用createBeanInstance方法實例化Bean,這個方法在後面會講解
3.若是當前RootBeanDefinition對象尚未調用過實現了的MergedBeanDefinitionPostProcessor接口的方法,則會進行調用
4.當知足如下三點
(1)是單例Bean
(2)嘗試解析bean之間的循環引用
(3)bean目前正在建立中
則會進一步檢查是否實現了SmartInstantiationAwareBeanPostProcessor接口若是實現了則調用是實現的getEarlyBeanReference方法
5.調用populateBean方法進行屬性填充,這裏後面會講解
6.調用initializeBean方法對Bean進行初始化,這裏後面會講解
2.1.1 實例化Bean,createBeanInstance
這裏的邏輯稍微有一點複雜,這個流程圖已是簡化事後的了。簡要根據代碼說明一下流程
protected BeanWrapper createBeanInstance(String beanName, RootBeanDefinition mbd, @Nullable Object[] args) { //步驟1 Class<?> beanClass = resolveBeanClass(mbd, beanName); if (beanClass != null && !Modifier.isPublic(beanClass.getModifiers()) && !mbd.isNonPublicAccessAllowed()) { throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Bean class isn't public, and non-public access not allowed: " + beanClass.getName()); } //步驟2 Supplier<?> instanceSupplier = mbd.getInstanceSupplier(); if (instanceSupplier != null) { return obtainFromSupplier(instanceSupplier, beanName); } //步驟3 if (mbd.getFactoryMethodName() != null) { return instantiateUsingFactoryMethod(beanName, mbd, args); } boolean resolved = false; boolean autowireNecessary = false; if (args == null) { synchronized (mbd.constructorArgumentLock) { if (mbd.resolvedConstructorOrFactoryMethod != null) { resolved = true; autowireNecessary = mbd.constructorArgumentsResolved; } } } //步驟4.1 if (resolved) { if (autowireNecessary) { return autowireConstructor(beanName, mbd, null, null); } else { return instantiateBean(beanName, mbd); } } //步驟4.2 Constructor<?>[] ctors = determineConstructorsFromBeanPostProcessors(beanClass, beanName); if (ctors != null || mbd.getResolvedAutowireMode() == AUTOWIRE_CONSTRUCTOR || mbd.hasConstructorArgumentValues() || !ObjectUtils.isEmpty(args)) { return autowireConstructor(beanName, mbd, ctors, args); } //步驟5 ctors = mbd.getPreferredConstructors(); if (ctors != null) { return autowireConstructor(beanName, mbd, ctors, null); } return instantiateBean(beanName, mbd); }
4.1 若是沒有傳入構造參數,則檢查是否存在已經緩存的無參構造器,有則使用構造器直接建立,沒有就會調用instantiateBean方法先獲取實例化的策略默認是CglibSubclassingInstantiationStrategy,而後實例化Bean。最後返回
4.2 若是傳入了構造參數,則會先檢查是否實現了SmartInstantiationAwareBeanPostProcessor接口,若是實現了會調用determineCandidateConstructors獲取返回的候選構造器。
4.3 檢查4個條件是否知足一個
(1)構造器不爲null
(2)從RootBeanDefinition中獲取到的關聯的注入方式是構造器注入(沒有構造參數就是setter注入,有則是構造器注入)
(3)含有構造參數
(4)getBean方法傳入構造參數不是空
知足其中一個則會調用返回的候選構造器實例化Bean並返回,若是都不知足,則會根據構造參數選則合適的有參構造器而後實例化Bean並返回
5.若是上面都沒有合適的構造器,則直接使用無參構造器建立並返回Bean。
2.1.2 填充Bean,populateBean
這裏仍是根據代碼來講一下流程
protected void populateBean(String beanName, RootBeanDefinition mbd, @Nullable BeanWrapper bw) { if (bw == null) { if (mbd.hasPropertyValues()) { throw new BeanCreationException( mbd.getResourceDescription(), beanName, "Cannot apply property values to null instance"); } else { // Skip property population phase for null instance. return; } } boolean continueWithPropertyPopulation = true; //步驟1 if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) { for (BeanPostProcessor bp : getBeanPostProcessors()) { if (bp instanceof InstantiationAwareBeanPostProcessor) { InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp; if (!ibp.postProcessAfterInstantiation(bw.getWrappedInstance(), beanName)) { continueWithPropertyPopulation = false; break; } } } } if (!continueWithPropertyPopulation) { return; } //步驟2-------------------- PropertyValues pvs = (mbd.hasPropertyValues() ? mbd.getPropertyValues() : null); int resolvedAutowireMode = mbd.getResolvedAutowireMode(); if (resolvedAutowireMode == AUTOWIRE_BY_NAME || resolvedAutowireMode == AUTOWIRE_BY_TYPE) { MutablePropertyValues newPvs = new MutablePropertyValues(pvs); // Add property values based on autowire by name if applicable. if (resolvedAutowireMode == AUTOWIRE_BY_NAME) { autowireByName(beanName, mbd, bw, newPvs); } // Add property values based on autowire by type if applicable. if (resolvedAutowireMode == AUTOWIRE_BY_TYPE) { autowireByType(beanName, mbd, bw, newPvs); } pvs = newPvs; } boolean hasInstAwareBpps = hasInstantiationAwareBeanPostProcessors(); boolean needsDepCheck = (mbd.getDependencyCheck() != AbstractBeanDefinition.DEPENDENCY_CHECK_NONE); PropertyDescriptor[] filteredPds = null; //步驟3 if (hasInstAwareBpps) { if (pvs == null) { pvs = mbd.getPropertyValues(); } for (BeanPostProcessor bp : getBeanPostProcessors()) { if (bp instanceof InstantiationAwareBeanPostProcessor) { InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp; PropertyValues pvsToUse = ibp.postProcessProperties(pvs, bw.getWrappedInstance(), beanName); if (pvsToUse == null) { if (filteredPds == null) { filteredPds = filterPropertyDescriptorsForDependencyCheck(bw, mbd.allowCaching); } pvsToUse = ibp.postProcessPropertyValues(pvs, filteredPds, bw.getWrappedInstance(), beanName); if (pvsToUse == null) { return; } } pvs = pvsToUse; } } } if (needsDepCheck) { if (filteredPds == null) { filteredPds = filterPropertyDescriptorsForDependencyCheck(bw, mbd.allowCaching); } checkDependencies(beanName, mbd, filteredPds, pvs); } //步驟4 if (pvs != null) { applyPropertyValues(beanName, mbd, bw, pvs); } }
2.1.3 初始化Bean,initializeBean
同時這裏根據代碼跟流程圖來講明
protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) { ....... beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this)); .......
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { if (!(bean instanceof EnvironmentAware || bean instanceof EmbeddedValueResolverAware || bean instanceof ResourceLoaderAware || bean instanceof ApplicationEventPublisherAware || bean instanceof MessageSourceAware || bean instanceof ApplicationContextAware)){ return bean; } AccessControlContext acc = null; if (System.getSecurityManager() != null) { acc = this.applicationContext.getBeanFactory().getAccessControlContext(); } if (acc != null) { AccessController.doPrivileged((PrivilegedAction<Object>) () -> { invokeAwareInterfaces(bean); return null; }, acc); } else { invokeAwareInterfaces(bean); } return bean; }
3.實例化Bean而後,檢查是否實現了InitializingBean的afterPropertiesSet方法,若是實現了就會調用
4.Bean不爲null而且bean不是合成的,若是實現了BeanPostProcessor的postProcessBeforeInitialization則會調用實現的postProcessAfterInitialization方法。到此建立Bean 的流程就沒了,剩下的就是容器銷燬的時候的了
Bean在建立完畢以後會檢查用戶是否指定了destroyMethodName以及是否實現了DestructionAwareBeanPostProcessor接口的requiresDestruction方法,若是指定了會記錄下來保存在DisposableBeanAdapter對象中並保存在bean的disposableBeans屬性中。代碼在AbstractBeanFactory的registerDisposableBeanIfNecessary中
protected void registerDisposableBeanIfNecessary(String beanName, Object bean, RootBeanDefinition mbd) { ...... registerDisposableBean(beanName, new DisposableBeanAdapter(bean, beanName, mbd, getBeanPostProcessors(), acc)); ...... }
public DisposableBeanAdapter(Object bean, String beanName, RootBeanDefinition beanDefinition, List<BeanPostProcessor> postProcessors, @Nullable AccessControlContext acc) { ....... String destroyMethodName = inferDestroyMethodIfNecessary(bean, beanDefinition); if (destroyMethodName != null && !(this.invokeDisposableBean && "destroy".equals(destroyMethodName)) && !beanDefinition.isExternallyManagedDestroyMethod(destroyMethodName)) { ...... this.destroyMethod = destroyMethod; } this.beanPostProcessors = filterPostProcessors(postProcessors, bean); }
在銷燬Bean的時候最後都會調用AbstractAutowireCapableBeanFactory的destroyBean方法。
public void destroyBean(Object existingBean) { new DisposableBeanAdapter(existingBean, getBeanPostProcessors(), getAccessControlContext()).destroy(); }
這裏是建立一個DisposableBeanAdapter對象,這個對象實現了Runnable接口,在實現的run方法中會調用實現的DisposableBean接口的destroy方法。而且在建立DisposableBeanAdapter對象的時候會根據傳入的bean是否實現了DisposableBean接口來設置invokeDisposableBean變量,這個變量表實有沒有實現DisposableBean接口
public DisposableBeanAdapter(Object bean, List<BeanPostProcessor> postProcessors, AccessControlContext acc) { Assert.notNull(bean, "Disposable bean must not be null"); this.bean = bean; this.beanName = bean.getClass().getName(); //根據傳入的bean是否實現了`DisposableBean`接口來設置`invokeDisposableBean`變量 this.invokeDisposableBean = (this.bean instanceof DisposableBean); this.nonPublicAccessAllowed = true; this.acc = acc; this.beanPostProcessors = filterPostProcessors(postProcessors, bean); } public void destroy() { ...... //根據invokeDisposableBean決定是否調用destroy方法 if (this.invokeDisposableBean) { if (logger.isTraceEnabled()) { logger.trace("Invoking destroy() on bean with name '" + this.beanName + "'"); } try { if (System.getSecurityManager() != null) { AccessController.doPrivileged((PrivilegedExceptionAction<Object>) () -> { ((DisposableBean) this.bean).destroy(); return null; }, this.acc); } else { ((DisposableBean) this.bean).destroy(); } } catch (Throwable ex) { String msg = "Invocation of destroy method failed on bean with name '" + this.beanName + "'"; if (logger.isDebugEnabled()) { logger.warn(msg, ex); } else { logger.warn(msg + ": " + ex); } } } ...... }
最後來一個大的流程
歡迎你們關注個人公衆號【風平浪靜如碼】,海量Java相關文章,學習資料都會在裏面更新,整理的資料也會放在裏面。
以爲寫的還不錯的就點個贊,加個關注唄!點關注,不迷路,持續更新!!!