您好,登錄后才能下訂單哦!
finishBeanFactoryInitialization(beanFactory):初始化不需要延遲實例化的單例bean
/* org/springframework/context/support/AbstractApplicationContext.java:870 */ protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory) {//初始化容器的conversionService if (beanFactory.containsBean(CONVERSION_SERVICE_BEAN_NAME) && beanFactory.isTypeMatch(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class)) { beanFactory.setConversionService( beanFactory.getBean(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class)); } //如果沒有任何一個類似PropertyPlaceholderConfigurer bean處理器 //默認一個嵌入式值解析器,主要是為了解析屬性值例如數據庫連接中${password}等屬性解析 if (!beanFactory.hasEmbeddedValueResolver()) { beanFactory.addEmbeddedValueResolver(strVal -> getEnvironment().resolvePlaceholders(strVal)); } // 先實例化LoadTimeWeaverAware相關的beans,以便盡早轉換,AOP相關 String[] weaverAwareNames = beanFactory.getBeanNamesForType(LoadTimeWeaverAware.class, false, false); for (String weaverAwareName : weaverAwareNames) { getBean(weaverAwareName); } //刪除臨時類加載器,停止臨時類加載器進行類型匹配 beanFactory.setTempClassLoader(null); //拷貝beandefinitionNames到frozenBeanDefinitionNames beanFactory.freezeConfiguration(); // 實例化所有不需要延遲初始化的單例bean beanFactory.preInstantiateSingletons(); }
beanFactory.preInstantiateSingletons():實例化所有不需要延遲初始化的單例bean
/* org/springframework/beans/factory/support/DefaultListableBeanFactory.java:885 */ public void preInstantiateSingletons() throws BeansException { if (logger.isTraceEnabled()) { logger.trace("Pre-instantiating singletons in " + this); } // Iterate over a copy to allow for init methods which in turn register new bean definitions. // While this may not be part of the regular factory bootstrap, it does otherwise work fine. //拷貝beanDefinitionNames副本,考慮到遍歷中其他線程可能修改beandefinitionNames // ArrayList類型遍歷時規避fast-fail,實現fast-safe List<String> beanNames = new ArrayList<>(this.beanDefinitionNames); // Trigger initialization of all non-lazy singleton beans... for (String beanName : beanNames) { //當bean存在parentBean時的繼承關系 //① bean不存在parentBean時,創建一個new RootBeanDefinition(beandefinition)返回。 //② bean存在parentBean時,支持繼承關系 // 需要先創建parentBean的new RootBeanDefinition(parentBeanDefinition) // 然后用beandefinition覆蓋parentBean的RootBeanDefinition的相關屬性并返回。superBean.overrideFrom(subBean); RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName); //用mergedLocalBeanDefinition判斷是否需要初始化 //篩選不是抽象類且不是延遲實例化的單例進行初始化 if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) { if (isFactoryBean(beanName)) { //工廠Bean beanName=&beanName Object bean = getBean(FACTORY_BEAN_PREFIX + beanName); if (bean instanceof FactoryBean) { final FactoryBean<?> factory = (FactoryBean<?>) bean; boolean isEagerInit; if (System.getSecurityManager() != null && factory instanceof SmartFactoryBean) { isEagerInit = AccessController.doPrivileged((PrivilegedAction<Boolean>) ((SmartFactoryBean<?>) factory)::isEagerInit, getAccessControlContext()); } else { isEagerInit = (factory instanceof SmartFactoryBean && ((SmartFactoryBean<?>) factory).isEagerInit()); } if (isEagerInit) { getBean(beanName); } } } else { //不是工廠bean 直接初始化 getBean(beanName); } } } //SmartInitializingSingleton類型的單例初始化后的回調 for (String beanName : beanNames) { Object singletonInstance = getSingleton(beanName); if (singletonInstance instanceof SmartInitializingSingleton) { final SmartInitializingSingleton smartSingleton = (SmartInitializingSingleton) singletonInstance; if (System.getSecurityManager() != null) { AccessController.doPrivileged((PrivilegedAction<Object>) () -> { smartSingleton.afterSingletonsInstantiated(); return null; }, getAccessControlContext()); } else { smartSingleton.afterSingletonsInstantiated(); } } } }
getBean(beanName):Bean初始化
/* org/springframework/beans/factory/support/AbstractBeanFactory.java:414 */ protected <T> T doGetBean(final String name, @Nullable final Class<T> requiredType, @Nullable final Object[] args, boolean typeCheckOnly) throws BeansException { //如果是aliaName,解析成beanName final String beanName = transformedBeanName(name); Object bean; // Eagerly check singleton cache for manually registered singletons. //檢查單例緩存中是否存在bean的實例化對象、初始化對象 (涉及循環依賴解決,需要先分清出Bean的兩個階段實例化、初始化) Object sharedInstance = getSingleton(beanName); if (sharedInstance != null && args == null) { if (logger.isTraceEnabled()) { if (isSingletonCurrentlyInCreation(beanName)) { logger.trace("Returning eagerly cached instance of singleton bean '" + beanName + "' that is not fully initialized yet - a consequence of a circular reference"); } else { logger.trace("Returning cached instance of singleton bean '" + beanName + "'"); } } bean = getObjectForBeanInstance(sharedInstance, name, beanName, null); } else { if (isPrototypeCurrentlyInCreation(beanName)) { //多例bean正在初始化時拋出異常 //由于多例bean,每調用一次getBean就會創建一個實例, //所以通過ThreadLocal打標,來避免單個線程的重復創建 throw new BeanCurrentlyInCreationException(beanName); } BeanFactory parentBeanFactory = getParentBeanFactory(); //父容器不為空,用父容器初始化 if (parentBeanFactory != null && !containsBeanDefinition(beanName)) { // Not found -> check parent. String nameToLookup = originalBeanName(name); if (parentBeanFactory instanceof AbstractBeanFactory) { return ((AbstractBeanFactory) parentBeanFactory).doGetBean( nameToLookup, requiredType, args, typeCheckOnly); } else if (args != null) { // Delegation to parent with explicit args. return (T) parentBeanFactory.getBean(nameToLookup, args); } else if (requiredType != null) { // No args -> delegate to standard getBean method. return parentBeanFactory.getBean(nameToLookup, requiredType); } else { return (T) parentBeanFactory.getBean(nameToLookup); } } if (!typeCheckOnly) { //alreadyCreated容器記錄已經被實例化的beanName, //這里將beanName添加到alreadyCreated markBeanAsCreated(beanName); } try { //重新獲取mergedLocalBeanDefinition(bean與parentBean合并) final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName); //檢查mbd不是抽象類 checkMergedBeanDefinition(mbd, beanName, args); //創建bean單例之前,需要保證先bean所依賴的Bean單例已創建, //這里的依賴對應@dependsOn聲明的依賴,這個注解不常用,這里也會出現一種循環依賴場景,與我們平常討論的引用循環依賴不同 //@dependsOn(value = "b") class A;@dependsOn(value = "a") class B //如果出現這種循環依賴 會直接拋出異常 啟動失敗 String[] dependsOn = mbd.getDependsOn(); if (dependsOn != null) { for (String dep : dependsOn) { if (isDependent(beanName, dep)) { throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Circular depends-on relationship between '" + beanName + "' and '" + dep + "'"); } //保存依賴關系到dependentBeanMap容器中 registerDependentBean(dep, beanName); try { //實例化依賴的bean 這里就是常見循環依賴地點 A依賴B B依賴A getBean(dep); } catch (NoSuchBeanDefinitionException ex) { throw new BeanCreationException(mbd.getResourceDescription(), beanName, "'" + beanName + "' depends on missing bean '" + dep + "'", ex); } } } // Create bean instance. if (mbd.isSingleton()) { //單例Bean創建 先取緩存,沒有才createBean sharedInstance = getSingleton(beanName, () -> { try { //創建Bean實例 return createBean(beanName, mbd, args); } catch (BeansException ex) { destroySingleton(beanName); throw ex; } }); bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd); } else if (mbd.isPrototype()) { //多例Bean創建 直接createbean Object prototypeInstance = null; try { //正在創建bean打標prototypesCurrentlyInCreation //與上面isPrototypeCurrentlyInCreation()聯合 beforePrototypeCreation(beanName); prototypeInstance = createBean(beanName, mbd, args); } finally { //正在創建bean打標prototypesCurrentlyInCreation刪除標記 afterPrototypeCreation(beanName); } bean = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd); } else { //自定義scope初始化、scope.get() String scopeName = mbd.getScope(); final Scope scope = this.scopes.get(scopeName); if (scope == null) { throw new IllegalStateException("No Scope registered for scope name '" + scopeName + "'"); } try { Object scopedInstance = scope.get(beanName, () -> { beforePrototypeCreation(beanName); try { return createBean(beanName, mbd, args); } finally { afterPrototypeCreation(beanName); } }); bean = getObjectForBeanInstance(scopedInstance, name, beanName, mbd); } catch (IllegalStateException ex) { throw new BeanCreationException(beanName, "Scope '" + scopeName + "' is not active for the current thread; consider " + "defining a scoped proxy for this bean if you intend to refer to it from a singleton", ex); } } } catch (BeansException ex) { cleanupAfterBeanCreationFailure(beanName); throw ex; } } //getBean(beanName,requiredType)時檢查創建的實例是否需要的類型,容器初始化時不會走進里面 if (requiredType != null && !requiredType.isInstance(bean)) { try { T convertedBean = getTypeConverter().convertIfNecessary(bean, requiredType); if (convertedBean == null) { throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass()); } return convertedBean; } catch (TypeMismatchException ex) { if (logger.isTraceEnabled()) { logger.trace("Failed to convert bean '" + name + "' to required type '" + ClassUtils.getQualifiedName(requiredType) + "'", ex); } throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass()); } } return (T) bean; }
sharedInstance = getSingleton(beanName, () -> {try {return createBean(beanName, mbd, args);}:單例初始化,這個方法比較簡單,主要關注下和循壞依賴相關的邏輯
/* org/springframework/beans/factory/support/DefaultSingletonBeanRegistry.java:201 */ public Object getSingleton(String beanName, ObjectFactory<?> singletonFactory) { Assert.notNull(beanName, "Bean name must not be null"); synchronized (this.singletonObjects) { Object singletonObject = this.singletonObjects.get(beanName); if (singletonObject == null) { if (this.singletonsCurrentlyInDestruction) { throw new BeanCreationNotAllowedException(beanName, "Singleton bean creation not allowed while singletons of this factory are in destruction " + "(Do not request a bean from a BeanFactory in a destroy method implementation!)"); } if (logger.isDebugEnabled()) { logger.debug("Creating shared instance of singleton bean '" + beanName + "'"); } //循環依賴相關:初始化前先singletonsCurrentlyInCreation.add(beanName) beforeSingletonCreation(beanName); boolean newSingleton = false; boolean recordSuppressedExceptions = (this.suppressedExceptions == null); if (recordSuppressedExceptions) { this.suppressedExceptions = new LinkedHashSet<>(); } try { //lamda表達式:其實是調用createBean(beanName, mbd, args):Bean初始化 singletonObject = singletonFactory.getObject(); newSingleton = true; } catch (IllegalStateException ex) { // Has the singleton object implicitly appeared in the meantime -> // if yes, proceed with it since the exception indicates that state. singletonObject = this.singletonObjects.get(beanName); if (singletonObject == null) { throw ex; } } catch (BeanCreationException ex) { if (recordSuppressedExceptions) { for (Exception suppressedException : this.suppressedExceptions) { ex.addRelatedCause(suppressedException); } } throw ex; } finally { if (recordSuppressedExceptions) { this.suppressedExceptions = null; } //循環依賴相關:初始化后singletonsCurrentlyInCreation.remove(beanName) afterSingletonCreation(beanName); } if (newSingleton) { //初始化完后 //this.singletonObjects.put(beanName, singletonObject);放入到單例容器中 //this.singletonFactories.remove(beanName);清空循環依賴相關的兩個打標 //this.earlySingletonObjects.remove(beanName); //this.registeredSingletons.add(beanName);放入單例beanName容器中 addSingleton(beanName, singletonObject); } } return singletonObject; } }
createBean(beanName, mbd, args):初始化實例,這個表方法注意一下,初始化實例前,留機會給BeanPostProcessor初始化代理類直接返回代理類初始化實例
/* org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java:467 */ protected Object createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args) throws BeanCreationException { if (logger.isTraceEnabled()) { logger.trace("Creating instance of bean '" + beanName + "'"); } RootBeanDefinition mbdToUse = mbd; Class<?> resolvedClass = resolveBeanClass(mbd, beanName); if (resolvedClass != null && !mbd.hasBeanClass() && mbd.getBeanClassName() != null) { //確保beanclass確實被解析了,若合并出錯,重新定義bean定義 mbdToUse = new RootBeanDefinition(mbd); mbdToUse.setBeanClass(resolvedClass); } // Prepare method overrides. try { //校驗需要重寫方法是否全部重寫,并且會給重寫方法是否重載打標 mbdToUse.prepareMethodOverrides(); } catch (BeanDefinitionValidationException ex) { throw new BeanDefinitionStoreException(mbdToUse.getResourceDescription(), beanName, "Validation of method overrides failed", ex); } try { //resolveBeforeInstantiation方法是調用BeanPostProcessor處理器 //后續研究一下BeanPostProcessor //這里是給AOP相關BeanPostProcessor處理器生成代理類實例的機會,直接生成初始化代理類實例返回 //會面研究AOP時發現,proxy object不是在這里初始化,這里只是提供一個機會 //proxy object具體是在的下面doCreateBean時里先創建target object //然后通過后置處理器的后置方法初始化的 Object bean = resolveBeforeInstantiation(beanName, mbdToUse); if (bean != null) { return bean; } } catch (Throwable ex) { throw new BeanCreationException(mbdToUse.getResourceDescription(), beanName, "BeanPostProcessor before instantiation of bean failed", ex); } try { //初始化實例 Object beanInstance = doCreateBean(beanName, mbdToUse, args); if (logger.isTraceEnabled()) { logger.trace("Finished creating instance of bean '" + beanName + "'"); } return beanInstance; } catch (BeanCreationException | ImplicitlyAppearedSingletonException ex) { // A previously detected exception with proper bean creation context already, // or illegal singleton state to be communicated up to DefaultSingletonBeanRegistry. throw ex; } catch (Throwable ex) { throw new BeanCreationException( mbdToUse.getResourceDescription(), beanName, "Unexpected exception during bean creation", ex); } }
doCreateBean(beanName,mbdToUse,args):beanClass實例化(即earlysingleton,注意不是初始化)然后DI完成初始化
/* org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java:549 */ protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final @Nullable Object[] args) throws BeanCreationException { // Instantiate the bean. BeanWrapper instanceWrapper = null; if (mbd.isSingleton()) { instanceWrapper = this.factoryBeanInstanceCache.remove(beanName); } if (instanceWrapper == null) { //反射調用beanClass的構造方法創建實例并包裝成beanwrapper-- instanceWrapper = createBeanInstance(beanName, mbd, args); } final Object bean = instanceWrapper.getWrappedInstance(); Class<?> beanType = instanceWrapper.getWrappedClass(); if (beanType != NullBean.class) { mbd.resolvedTargetType = beanType; } // Allow post-processors to modify the merged bean definition. //允許MergedBeanDefinitionPostProcessors后置管理器修改rootBeandefinition 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"); } //循環依賴相關邏輯: //this.singletonFactories.put(beanName, singletonFactory); //將實例化bean、beanName組裝成singletonFactory裝入singletonFactories容器 //this.earlySingletonObjects.remove(beanName); //刪除earlySingletonObjects中beanName addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean)); } // Initialize the bean instance. //創建實例 Object exposedObject = bean; try { //實例初始化 就是在這里面實現依賴注入DI的: //具體是調用AutowiredAnnotationBeanPostProcessor.postProcessProperties populateBean(beanName, mbd, instanceWrapper);//調用Bean后置管理器前置方法BeanPostProcessor.postProcessBeforeInitialization //instanceof aware接口,setBeanName setBeanClassLoader setBeanFactory //調用Bean后置管理器后置方法BeanPostProcessor.postProcessAfterInitialization 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); } } //... return exposedObject; }
populateBean(beanName, mbd, instanceWrapper):依賴注入DI執行時機
/* org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java:1381 */ protected void populateBean(String beanName, RootBeanDefinition mbd, @Nullable BeanWrapper bw) { //... PropertyDescriptor[] filteredPds = null; if (hasInstAwareBpps) { if (pvs == null) { pvs = mbd.getPropertyValues(); } for (BeanPostProcessor bp : getBeanPostProcessors()) { if (bp instanceof InstantiationAwareBeanPostProcessor) { InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp; //這里調用AutowiredAnnotationBeanPostProcessor.postProcessProperties() //完成注解自動注入(DI) 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); } if (pvs != null) { applyPropertyValues(beanName, mbd, bw, pvs); } }
DI結束,bean初始化基本結束了。然后就是bean初始化的BeanPostProcessor執行前置后置方法
BeanPostProcessor前置后置方法:一些很重要架構都需要依靠實現,例如AOP動態代理產生代理對象
AnnotationAwareAspectJAutoProxyCreator.postProcessAfterInitialization
/* org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#initializeBean(java.lang.String, java.lang.Object, org.springframework.beans.factory.support.RootBeanDefinition) */ protected Object initializeBean(final String beanName, final Object bean, @Nullable RootBeanDefinition mbd) { if (System.getSecurityManager() != null) { AccessController.doPrivileged((PrivilegedAction<Object>) () -> { invokeAwareMethods(beanName, bean); return null; }, getAccessControlContext()); } else { invokeAwareMethods(beanName, bean); } Object wrappedBean = bean; if (mbd == null || !mbd.isSynthetic()) { //調用所有BeanPostProcessor前置方法BeanPostProcessor.postProcessBeforeInitialization wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName); } try { invokeInitMethods(beanName, wrappedBean, mbd); } catch (Throwable ex) { throw new BeanCreationException( (mbd != null ? mbd.getResourceDescription() : null), beanName, "Invocation of init method failed", ex); } if (mbd == null || !mbd.isSynthetic()) { //調用所有BeanPostProcessor前置方法BeanPostProcessor.postProcessAfterInitialization //例如AOP代理對象就是這里初始化的,AnnotationAwareAspectJAutoProxyCreator.postProcessAfterInitialization wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName); } return wrappedBean; }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。