在前面咱們分析了最底層的IOC容器BeanFactory,接着簡單分析了高級形態的容器ApplicationContext,在ApplicationContext 中咱們知道一個核心方法 refresh,這裏面就是IOC容器的初始化流程,在前面並無直接去分析它,只是簡單的分析了BeanDefinition的載入,解析註冊,有了這些基礎後,再來完整的分析IOC容器的啓動流程。java
在 AbstractApplicationContext 中以及將refresh整個流程定義出來了,咱們再來看refresh 源碼。spring
@Override public void refresh() throws BeansException, IllegalStateException { synchronized (this.startupShutdownMonitor) { // Prepare this context for refreshing. prepareRefresh(); // Tell the subclass to refresh the internal bean factory. ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory(); // Prepare the bean factory for use in this context. prepareBeanFactory(beanFactory); try { // Allows post-processing of the bean factory in context subclasses. postProcessBeanFactory(beanFactory); // Invoke factory processors registered as beans in the context. invokeBeanFactoryPostProcessors(beanFactory); // Register bean processors that intercept bean creation. registerBeanPostProcessors(beanFactory); // Initialize message source for this context. initMessageSource(); // Initialize event multicaster for this context. initApplicationEventMulticaster(); // Initialize other special beans in specific context subclasses. onRefresh(); // Check for listener beans and register them. registerListeners(); // Instantiate all remaining (non-lazy-init) singletons. finishBeanFactoryInitialization(beanFactory); // Last step: publish corresponding event. finishRefresh(); } catch (BeansException ex) { if (logger.isWarnEnabled()) { logger.warn("Exception encountered during context initialization - " + "cancelling refresh attempt: " + ex); } // Destroy already created singletons to avoid dangling resources. destroyBeans(); // Reset 'active' flag. cancelRefresh(ex); // Propagate exception to caller. throw ex; } finally { // Reset common introspection caches in Spring's core, since we // might not ever need metadata for singleton beans anymore... resetCommonCaches(); } } }
下面咱們針對refresh裏面的執行流程一個一個的進行分析,來看它作了什麼而且是如何實現的。緩存
protected void prepareRefresh() { // 記錄啓動時間, this.startupDate = System.currentTimeMillis(); // 設置容器狀態 this.closed.set(false); this.active.set(true); if (logger.isInfoEnabled()) { logger.info("Refreshing " + this); } // 初始化一些屬性設置(一個空方法,留給子類具體實現) initPropertySources(); // 校驗屬性的合法性 getEnvironment().validateRequiredProperties(); //保存容器中一些早期的事件 this.earlyApplicationEvents = new LinkedHashSet<ApplicationEvent>(); }
refresh方法中的下一行 obtainFreshBeanFactory()。session
注意,這個方法很重要,這裏將會初始化 BeanFactory、加載 Bean、註冊 Bean 等等。app
AbstractApplicationContext -> obtainFreshBeanFactory:ide
protected ConfigurableListableBeanFactory obtainFreshBeanFactory() { // 關閉舊的 BeanFactory (若是有),建立新的 BeanFactory,加載 Bean 定義、註冊 Bean 等等 refreshBeanFactory(); ConfigurableListableBeanFactory beanFactory = getBeanFactory(); if (logger.isDebugEnabled()) { logger.debug("Bean factory for " + getDisplayName() + ": " + beanFactory); } return beanFactory; }
AbstractRefreshableApplicationContext->refreshBeanFactory:函數
@Override protected final void refreshBeanFactory() throws BeansException { if (hasBeanFactory()) { destroyBeans(); closeBeanFactory(); } try { // 初始化一個 DefaultListableBeanFactory DefaultListableBeanFactory beanFactory = createBeanFactory(); // 用於 BeanFactory 的序列化 beanFactory.setSerializationId(getId()); // 設置 BeanFactory 的兩個配置屬性:是否容許 Bean 覆蓋、是否容許循環引用 customizeBeanFactory(beanFactory); // 加載 Bean 到 BeanFactory 中 loadBeanDefinitions(beanFactory); synchronized (this.beanFactoryMonitor) { this.beanFactory = beanFactory; } } catch (IOException ex) { throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex); } }
這裏主要看一下 customizeBeanFactory 方法:源碼分析
protected void customizeBeanFactory(DefaultListableBeanFactory beanFactory) { if (this.allowBeanDefinitionOverriding != null) { // 是否容許 Bean 定義覆蓋 beanFactory.setAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding); } if (this.allowCircularReferences != null) { // 是否容許 Bean 間的循環依賴 beanFactory.setAllowCircularReferences(this.allowCircularReferences); } }
BeanFactory預準備工做post
protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) { //設置類加載器 beanFactory.setBeanClassLoader(getClassLoader()); //設置支持表達式的解析器 beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader())); beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment())); /** 添加一個 BeanPostProcessor,這個 processor 比較簡單: 實現了 Aware 接口的 beans 在初始化的時候,這個 processor 負責回調, 這個咱們很經常使用,如咱們會爲了獲取 ApplicationContext 而 implement ApplicationContextAware 注意:它不只僅回調 ApplicationContextAware, 還會負責回調 EnvironmentAware、ResourceLoaderAware 等,看下源碼就清楚了 */ beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this)); // 下面幾行的意思就是,若是某個 bean 依賴於如下幾個接口的實現類,在自動裝配的時候忽略它們, // Spring 會經過其餘方式來處理這些依賴。 beanFactory.ignoreDependencyInterface(EnvironmentAware.class); beanFactory.ignoreDependencyInterface(EmbeddedValueResolverAware.class); beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class); beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class); beanFactory.ignoreDependencyInterface(MessageSourceAware.class); beanFactory.ignoreDependencyInterface(ApplicationContextAware.class); // BeanFactory interface not registered as resolvable type in a plain factory. // MessageSource registered (and found for autowiring) as a bean. beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory); beanFactory.registerResolvableDependency(ResourceLoader.class, this); beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this); beanFactory.registerResolvableDependency(ApplicationContext.class, this); // 這個 BeanPostProcessor 也很簡單,在 bean 實例化後,若是是 ApplicationListener 的子類, // 那麼將其添加到 listener 列表中,能夠理解成:註冊 事件監聽器 beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this)); // Detect a LoadTimeWeaver and prepare for weaving, if found. if (beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) { beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory)); // Set a temporary ClassLoader for type matching. beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader())); } // Register default environment beans. if (!beanFactory.containsLocalBean(ENVIRONMENT_BEAN_NAME)) { beanFactory.registerSingleton(ENVIRONMENT_BEAN_NAME, getEnvironment()); } if (!beanFactory.containsLocalBean(SYSTEM_PROPERTIES_BEAN_NAME)) { beanFactory.registerSingleton(SYSTEM_PROPERTIES_BEAN_NAME, getEnvironment().getSystemProperties()); } if (!beanFactory.containsLocalBean(SYSTEM_ENVIRONMENT_BEAN_NAME)) { beanFactory.registerSingleton(SYSTEM_ENVIRONMENT_BEAN_NAME, getEnvironment().getSystemEnvironment()); } }
這個方法的做用主要是完成BeanFactory準備工做完成以後進行的後置處理工做,不要和BeanPostProcessor搞混了。測試
若是有 Bean 實現了BeanFactoryPostProcessor接口,那麼在容器初始化之後,Spring 會負責調用裏面的 postProcessBeanFactory 方法。
這裏是提供給子類的擴展點,子類經過重寫這個方法在BeanFactory建立並預準備完成之後作進一步的設置。
這個方法就是調用調用 BeanFactoryPostProcessor 各個實現類的 postProcessBeanFactory(factory) 方法
protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) { PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors()); // Detect a LoadTimeWeaver and prepare for weaving, if found in the meantime // (e.g. through an @Bean method registered by ConfigurationClassPostProcessor) if (beanFactory.getTempClassLoader() == null && beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) { beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory)); beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader())); } }
咱們主要來看PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors())這一句。
public static void invokeBeanFactoryPostProcessors( ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) { Set<String> processedBeans = new HashSet<>(); //先判斷當前的BeanFactory是否是BeanDefinitionRegistry(DefaultListableBeanFactory是BeanDefinitionRegistry) if (beanFactory instanceof BeanDefinitionRegistry) { BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory; List<BeanFactoryPostProcessor> regularPostProcessors = new ArrayList<>(); List<BeanDefinitionRegistryPostProcessor> registryProcessors = new ArrayList<>(); //拿到全部的BeanFactoryPostProcessor在這進行遍歷(通常第一次刷新容器不會走下面這個for循環,由於此時容器中尚未將BeanFactoryPostProcessor加入它對應的集合中,因此傳入參數beanFactoryPostProcessors爲空) for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) { if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) { BeanDefinitionRegistryPostProcessor registryProcessor = (BeanDefinitionRegistryPostProcessor) postProcessor; registryProcessor.postProcessBeanDefinitionRegistry(registry); registryProcessors.add(registryProcessor); } else { regularPostProcessors.add(postProcessor); } } // Do not initialize FactoryBeans here: We need to leave all regular beans // uninitialized to let the bean factory post-processors apply to them! // Separate between BeanDefinitionRegistryPostProcessors that implement // PriorityOrdered, Ordered, and the rest. List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors = new ArrayList<>(); //首先,從容器中獲取全部類型爲BeanDefinitionRegistryPostProcessor的bean String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false); //而後先遍歷執行實現PriorityOrdered接口的BeanDefinitionRegistryPostProcessor的接口方法 for (String ppName : postProcessorNames) { if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) { currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class)); processedBeans.add(ppName); } } sortPostProcessors(currentRegistryProcessors, beanFactory); registryProcessors.addAll(currentRegistryProcessors); invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry); currentRegistryProcessors.clear(); //下來再執行實現了Ordered接口的BeanDefinitionRegistryPostProcessor的接口方法 postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false); for (String ppName : postProcessorNames) { if (!processedBeans.contains(ppName) && beanFactory.isTypeMatch(ppName, Ordered.class)) { currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class)); processedBeans.add(ppName); } } sortPostProcessors(currentRegistryProcessors, beanFactory); registryProcessors.addAll(currentRegistryProcessors); invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry); currentRegistryProcessors.clear(); //最後執行沒有實現任何接口的 boolean reiterate = true; while (reiterate) { reiterate = false; postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false); for (String ppName : postProcessorNames) { if (!processedBeans.contains(ppName)) { currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class)); processedBeans.add(ppName); reiterate = true; } } sortPostProcessors(currentRegistryProcessors, beanFactory); registryProcessors.addAll(currentRegistryProcessors); invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry); currentRegistryProcessors.clear(); } //---------------------------------------------------------------------------------- //下面就和上面同樣的模式來執行容器中的BeanFactoryPostProcessor接口實現類的接口方法 // Now, invoke the postProcessBeanFactory callback of all processors handled so far. invokeBeanFactoryPostProcessors(registryProcessors, beanFactory); invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory); } else { // Invoke factory processors registered with the context instance. invokeBeanFactoryPostProcessors(beanFactoryPostProcessors, beanFactory); } // Do not initialize FactoryBeans here: We need to leave all regular beans // uninitialized to let the bean factory post-processors apply to them! String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false); // Separate between BeanFactoryPostProcessors that implement PriorityOrdered, // Ordered, and the rest. List<BeanFactoryPostProcessor> priorityOrderedPostProcessors = new ArrayList<>(); List<String> orderedPostProcessorNames = new ArrayList<>(); List<String> nonOrderedPostProcessorNames = new ArrayList<>(); for (String ppName : postProcessorNames) { if (processedBeans.contains(ppName)) { // skip - already processed in first phase above } else if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) { priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanFactoryPostProcessor.class)); } else if (beanFactory.isTypeMatch(ppName, Ordered.class)) { orderedPostProcessorNames.add(ppName); } else { nonOrderedPostProcessorNames.add(ppName); } } // First, invoke the BeanFactoryPostProcessors that implement PriorityOrdered. sortPostProcessors(priorityOrderedPostProcessors, beanFactory); invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory); // Next, invoke the BeanFactoryPostProcessors that implement Ordered. List<BeanFactoryPostProcessor> orderedPostProcessors = new ArrayList<>(); for (String postProcessorName : orderedPostProcessorNames) { orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class)); } sortPostProcessors(orderedPostProcessors, beanFactory); invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory); // Finally, invoke all other BeanFactoryPostProcessors. List<BeanFactoryPostProcessor> nonOrderedPostProcessors = new ArrayList<>(); for (String postProcessorName : nonOrderedPostProcessorNames) { nonOrderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class)); } invokeBeanFactoryPostProcessors(nonOrderedPostProcessors, beanFactory); // Clear cached merged bean definitions since the post-processors might have // modified the original metadata, e.g. replacing placeholders in values... beanFactory.clearMetadataCache(); }
這個方法是用來註冊BeanPostProcessor(Bean的後置處理器),來攔截Bean的建立過程。
這裏也就不展開了,和處理 BeanFactoryPostProcessors 相似按照優先級註冊,只是這裏註冊BeanPostProcessors ,並無執行其方法,具體的執行時機是在bean實例化的時候。
初始化MessageSource組件,這個主要是用來完成國際化、消息綁定、以及消息解析功能的。
初始化事件派發器和註冊事件監聽器,這兩個方法在以前的文章中有分析過,這裏就再也不作分析了。
這個方法很重要,做用是來完成剩下的非懶加載的單實例Bean
protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory) { // 首先,初始化名字爲 conversionService 的 Bean。這裏暫時不講 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)); } //無論 if (!beanFactory.hasEmbeddedValueResolver()) { beanFactory.addEmbeddedValueResolver(strVal -> getEnvironment().resolvePlaceholders(strVal)); } // 這是 AspectJ 相關的內容,先無論 String[] weaverAwareNames = beanFactory.getBeanNamesForType(LoadTimeWeaverAware.class, false, false); for (String weaverAwareName : weaverAwareNames) { getBean(weaverAwareName); } // Stop using the temporary ClassLoader for type matching. beanFactory.setTempClassLoader(null); // 凍結 BeanDefinition,再也不修改配置了 beanFactory.freezeConfiguration(); // Instantiate all remaining (non-lazy-init) singletons. // 開始初始化 單例bean beanFactory.preInstantiateSingletons(); }
DefaultListableBeanFactory-> preInstantiateSingletons:這個方法纔是開始初始化單例bean
public void preInstantiateSingletons() throws BeansException { // this.beanDefinitionNames 保存了全部的 beanNames List<String> beanNames = new ArrayList<>(this.beanDefinitionNames); // 觸發全部的非懶加載的 singleton beans 的初始化操做 for (String beanName : beanNames) { RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName); // 非抽象、非懶加載的 singletons。若是配置了 'abstract = true',那是不須要初始化的 if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) { // 處理 FactoryBean if (isFactoryBean(beanName)) { // FactoryBean 的話,在 beanName 前面加上 ‘&’ 符號。再調用 getBean Object bean = getBean(FACTORY_BEAN_PREFIX + beanName); if (bean instanceof FactoryBean) { final FactoryBean<?> factory = (FactoryBean<?>) bean; boolean isEagerInit; // 判斷當前 FactoryBean 是不是 SmartFactoryBean 的實現,忽略 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) 這個方法就能夠進行初始化了 getBean(beanName); } } } // 到這裏說明全部的非懶加載的 singleton beans 已經完成了初始化 // 若是咱們定義的 bean 是實現了 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(); } } } }
大多數bean都是經過這個方法來進行初始化的
AbstractBeanFactory->getBean:
@Override public Object getBean(String name) throws BeansException { return doGetBean(name, null, null, false); }
doGetBean:
protected <T> T doGetBean(final String name, final Class<T> requiredType, final Object[] args, boolean typeCheckOnly) throws BeansException { // 獲取一個 標準的 beanName,處理兩種狀況: //一個是前面說的 FactoryBean(前面帶 ‘&’), //若是指定的是別名,將別名轉換爲規範的Bean名稱 final String beanName = transformedBeanName(name); Object bean; // 檢查下是否是已經存在了,若是已經建立了的單例bean,會放入Map 中 Object sharedInstance = getSingleton(beanName); // 可是若是 args 不爲空的時候,那麼不論是否該bean已經存在都會從新建立 if (sharedInstance != null && args == null) { // 下面這個方法:若是是普通 Bean 的話,直接返回 sharedInstance, // 若是是 FactoryBean 的話,返回它建立的那個實例對象,調用FactoryBean的getObject 方法 //這裏就不展開了 bean = getObjectForBeanInstance(sharedInstance, name, beanName, null); } else { // 建立過了此 beanName 的 prototype 類型的 bean,那麼拋異常, // 每每是由於陷入了循環引用 if (isPrototypeCurrentlyInCreation(beanName)) { throw new BeanCurrentlyInCreationException(beanName); } // 檢查一下這個 BeanDefinition 在容器中是否存在 BeanFactory parentBeanFactory = getParentBeanFactory(); if (parentBeanFactory != null && !containsBeanDefinition(beanName)) { // 若是當前容器不存在這個 BeanDefinition,看看父容器中有沒有 String nameToLookup = originalBeanName(name); if (parentBeanFactory instanceof AbstractBeanFactory) { return ((AbstractBeanFactory) parentBeanFactory).doGetBean( nameToLookup, requiredType, args, typeCheckOnly); } else if (args != null) { return (T) parentBeanFactory.getBean(nameToLookup, args); } else { // No args -> delegate to standard getBean method. return parentBeanFactory.getBean(nameToLookup, requiredType); } } if (!typeCheckOnly) { // typeCheckOnly 爲 false,將當前 beanName 放入一個 alreadyCreated 的 Set 集合中,標記一下。 markBeanAsCreated(beanName); } /* * 到這裏的話,要準備建立 Bean 了, * 對於 singleton 的 Bean 來講,容器中還沒建立過此 Bean; * 對於 prototype 的 Bean 來講,原本就是要建立一個新的 Bean。 */ try { //根據指定Bean名稱獲取其父級的Bean定義,主要解決Bean繼承時子類 //合併父類公共屬性問題 final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName); checkMergedBeanDefinition(mbd, beanName, args); // 先初始化依賴的全部 Bean //檢查是否是有循環依賴,這裏的循環依賴和咱們前面說的循環依賴又不同 //這裏的依賴指的是 depends-on 中定義的依賴 //depends-on用來表示一個Bean的實例化依靠另外一個Bean先實例化。 //若是在一個bean A上定義了depend-on B那麼就表示:A 實例化前先實例化 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 + "'"); } // 註冊一下依賴關係 registerDependentBean(dep, beanName); try { //遞歸調用getBean方法,獲取當前Bean的依賴Bean getBean(dep); } catch (NoSuchBeanDefinitionException ex) { throw new BeanCreationException(mbd.getResourceDescription(), beanName, "'" + beanName + "' depends on missing bean '" + dep + "'", ex); } } } // 若是是 singleton scope 的,建立 singleton 的實例 if (mbd.isSingleton()) { sharedInstance = getSingleton(beanName, () -> { try { return createBean(beanName, mbd, args); } catch (BeansException ex) { // Explicitly remove instance from singleton cache: It might have been put there // eagerly by the creation process, to allow for circular reference resolution. // Also remove any beans that received a temporary reference to the bean. destroySingleton(beanName); throw ex; } }); bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd); } // 若是是 prototype scope 的,建立 prototype 的實例 else if (mbd.isPrototype()) { // It's a prototype -> create a new instance. Object prototypeInstance = null; try { beforePrototypeCreation(beanName); prototypeInstance = createBean(beanName, mbd, args); } finally { afterPrototypeCreation(beanName); } bean = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd); } else { // 若是不是 singleton 和 prototype 的話,須要委託給相應的實現類來處理 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方法 beforePrototypeCreation(beanName); try { return createBean(beanName, mbd, args); } finally { //回調afterPrototypeCreation方法 afterPrototypeCreation(beanName); } }); //獲取給定Bean的實例對象 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; } } //對建立的Bean實例對象進行類型檢查 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.isDebugEnabled()) { logger.debug("Failed to convert bean '" + name + "' to required type '" + ClassUtils.getQualifiedName(requiredType) + "'", ex); } throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass()); } } return (T) bean; }
上面,咱們能夠看見在建立實例時作了判斷
經過上面的代碼基本上理解大概邏輯是不成問題的,接下來確定就是分析createBean 方法了。
AbstractAutowireCapableBeanFactory->createBean:
protected Object createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args) throws BeanCreationException { if (logger.isDebugEnabled()) { logger.debug("Creating instance of bean '" + beanName + "'"); } RootBeanDefinition mbdToUse = mbd; // 確保 BeanDefinition 中的 Class 被加載 Class<?> resolvedClass = resolveBeanClass(mbd, beanName); if (resolvedClass != null && !mbd.hasBeanClass() && mbd.getBeanClassName() != null) { mbdToUse = new RootBeanDefinition(mbd); mbdToUse.setBeanClass(resolvedClass); } // 準備方法覆寫,這裏又涉及到一個概念:MethodOverrides, // 它來自於 bean 定義中的 <replaced-method /> // 沒怎麼了解 try { mbdToUse.prepareMethodOverrides(); } catch (BeanDefinitionValidationException ex) { throw new BeanDefinitionStoreException(mbdToUse.getResourceDescription(), beanName, "Validation of method overrides failed", ex); } try { // Give BeanPostProcessors a chance to return a proxy instead of the target bean instance. //若是Bean配置了初始化前和初始化後的處理器,則試圖返回一個代理對象 //這個方法在以前的文章中也有解析 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 { //建立 bean Object beanInstance = doCreateBean(beanName, mbdToUse, args); return beanInstance; } catch (BeanCreationException | ImplicitlyAppearedSingletonException ex) { throw ex; } catch (Throwable ex) { throw new BeanCreationException( mbdToUse.getResourceDescription(), beanName, "Unexpected exception during bean creation", ex); } }
doCreateBean
protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final @Nullable Object[] args) throws BeanCreationException { // Instantiate the bean. BeanWrapper instanceWrapper = null; if (mbd.isSingleton()) { //移除BeanWrapper緩存 instanceWrapper = this.factoryBeanInstanceCache.remove(beanName); } if (instanceWrapper == null) { //建立 BeanWrapper instanceWrapper = createBeanInstance(beanName, mbd, args); } //得到bean 實例 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. //調用PostProcessor後置處理器 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)); //提早曝光bean if (earlySingletonExposure) { addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean)); } // Initialize the bean instance. Object exposedObject = bean; try { //實例化後,須要進行屬性的賦值 populateBean(beanName, mbd, instanceWrapper); // 這裏就是處理 bean 初始化完成後的各類回調,例如init-method 配置,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) { //若是已經提交曝光了bean,那麼就從緩存中獲取bean Object earlySingletonReference = getSingleton(beanName, false); if (earlySingletonReference != null) { //根據名稱獲取的以註冊的Bean和正在實例化的Bean是同一個 if (exposedObject == bean) { exposedObject = earlySingletonReference; } else if (!this.allowRawInjectionDespiteWrapping && hasDependentBean(beanName)) { String[] dependentBeans = getDependentBeans(beanName); Set<String> actualDependentBeans = new LinkedHashSet<>(dependentBeans.length); for (String dependentBean : dependentBeans) { if (!removeSingletonIfCreatedForTypeCheckOnly(dependentBean)) { actualDependentBeans.add(dependentBean); } } if (!actualDependentBeans.isEmpty()) { throw new BeanCurrentlyInCreationException(beanName, "Bean with name '" + beanName + "' has been injected into other beans [" + StringUtils.collectionToCommaDelimitedString(actualDependentBeans) + "] in its raw version as part of a circular reference, but has eventually been " + "wrapped. This means that said other beans do not use the final version of the " + "bean. This is often the result of over-eager type matching - consider using " + "'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example."); } } } } // Register bean as disposable. try { registerDisposableBeanIfNecessary(beanName, bean, mbd); } catch (BeanDefinitionValidationException ex) { throw new BeanCreationException( mbd.getResourceDescription(), beanName, "Invalid destruction signature", ex); } return exposedObject; }
到這裏,基本上簡單的分析了 doCreateBean 方法,整個bean就已經初始化完成了,這裏面有三個重點的方法(過程)
一、建立 Bean 實例(createBeanInstance) 方法,
二、依賴注入(populateBean) 方法,
三、一系列初始化或者回調(initializeBean)。
createBeanInstance:
protected BeanWrapper createBeanInstance(String beanName, RootBeanDefinition mbd, @Nullable Object[] args) { // Make sure bean class is actually resolved at this point. 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()); } Supplier<?> instanceSupplier = mbd.getInstanceSupplier(); if (instanceSupplier != null) { return obtainFromSupplier(instanceSupplier, beanName); } if (mbd.getFactoryMethodName() != null) { // 採用工廠方法實例化,配置 factory-method return instantiateUsingFactoryMethod(beanName, mbd, args); } // Shortcut when re-creating the same bean... // 若是不是第一次建立,好比第二次建立 prototype bean。 // 這種狀況下,咱們能夠從第一次建立知道,採用無參構造函數,仍是構造函數依賴注入 來完成實例化 // 這個能夠經過代碼來測試,屢次經過getbean(name)來獲取 prototype的bean boolean resolved = false; boolean autowireNecessary = false; if (args == null) { synchronized (mbd.constructorArgumentLock) { if (mbd.resolvedConstructorOrFactoryMethod != null) { resolved = true; autowireNecessary = mbd.constructorArgumentsResolved; } } } if (resolved) { if (autowireNecessary) { //配置了自動裝配屬性,使用容器的自動裝配實例化 //容器的自動裝配是根據參數類型匹配Bean的構造方法 return autowireConstructor(beanName, mbd, null, null); } else { //使用默認的無參構造方法實例化 return instantiateBean(beanName, mbd); } } // 判斷是否採用有參構造函數. Constructor<?>[] ctors = determineConstructorsFromBeanPostProcessors(beanClass, beanName); if (ctors != null || mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_CONSTRUCTOR || mbd.hasConstructorArgumentValues() || !ObjectUtils.isEmpty(args)) { //使用容器的自動裝配特性,調用匹配的構造方法實例化 return autowireConstructor(beanName, mbd, ctors, args); } //使用默認的無參構造方法實例化 return instantiateBean(beanName, mbd); }
instantiateBean:
protected BeanWrapper instantiateBean(final String beanName, final RootBeanDefinition mbd) { try { Object beanInstance; final BeanFactory parent = this; if (System.getSecurityManager() != null) { beanInstance = AccessController.doPrivileged((PrivilegedAction<Object>) () -> getInstantiationStrategy().instantiate(mbd, beanName, parent), getAccessControlContext()); } else { //實例化 beanInstance = getInstantiationStrategy().instantiate(mbd, beanName, parent); } // 包裝一下 BeanWrapper bw = new BeanWrapperImpl(beanInstance); //初始化BeanWrapper //會設置 conversionService,註冊customEditors initBeanWrapper(bw); return bw; } catch (Throwable ex) { throw new BeanCreationException( mbd.getResourceDescription(), beanName, "Instantiation of bean failed", ex); } }
到這裏,bean的實例就算是建立完成了。
populateBean
爲bean的屬性進行賦值
AbstractAutowireCapableBeanFactory->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; } } // Give any InstantiationAwareBeanPostProcessors the opportunity to modify the // state of the bean before properties are set. This can be used, for example, // to support styles of field injection. boolean continueWithPropertyPopulation = true; if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) { for (BeanPostProcessor bp : getBeanPostProcessors()) { //InstantiationAwareBeanPostProcessor 在實例前和實例後進行回調處理 if (bp instanceof InstantiationAwareBeanPostProcessor) { InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp; //在設置屬性以前調用Bean的PostProcessor後置處理器 if (!ibp.postProcessAfterInstantiation(bw.getWrappedInstance(), beanName)) { continueWithPropertyPopulation = false; break; } } } } if (!continueWithPropertyPopulation) { return; } // bean 實例的全部屬性 PropertyValues pvs = (mbd.hasPropertyValues() ? mbd.getPropertyValues() : null); if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_NAME || mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_TYPE) { MutablePropertyValues newPvs = new MutablePropertyValues(pvs); // Add property values based on autowire by name if applicable. if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_NAME) { // 經過名字找到全部屬性值,若是是 bean 依賴,先初始化依賴的 bean。記錄依賴關係 autowireByName(beanName, mbd, bw, newPvs); } // Add property values based on autowire by type if applicable. if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_TYPE) { // 經過類型裝配 autowireByType(beanName, mbd, bw, newPvs); } pvs = newPvs; } //檢查容器是否持有用於處理單態模式Bean關閉時的後置處理器 boolean hasInstAwareBpps = hasInstantiationAwareBeanPostProcessors(); //Bean實例對象沒有依賴(此依賴是depends-on),即沒有繼承基類 boolean needsDepCheck = (mbd.getDependencyCheck() != RootBeanDefinition.DEPENDENCY_CHECK_NONE); if (hasInstAwareBpps || needsDepCheck) { if (pvs == null) { pvs = mbd.getPropertyValues(); } PropertyDescriptor[] filteredPds = filterPropertyDescriptorsForDependencyCheck(bw, mbd.allowCaching); if (hasInstAwareBpps) { for (BeanPostProcessor bp : getBeanPostProcessors()) { //處理特殊的BeanPostProcessor if (bp instanceof InstantiationAwareBeanPostProcessor) { InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp; //使用BeanPostProcessor處理器處理屬性值 pvs = ibp.postProcessPropertyValues(pvs, filteredPds, bw.getWrappedInstance(), beanName); if (pvs == null) { return; } } } } if (needsDepCheck) { //爲要設置的屬性進行依賴檢查 checkDependencies(beanName, mbd, filteredPds, pvs); } } if (pvs != null) { // 設置 bean 實例的屬性值 applyPropertyValues(beanName, mbd, bw, pvs); } }
initializeBean
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 { // 若是 bean 實現了 BeanNameAware、BeanClassLoaderAware 或 BeanFactoryAware 接口,回調 invokeAwareMethods(beanName, bean); } Object wrappedBean = bean; if (mbd == null || !mbd.isSynthetic()) { // BeanPostProcessor 的 postProcessBeforeInitialization 回調 wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName); } try { // 處理 bean 中定義的 init-method, // 或者若是 bean 實現了 InitializingBean 接口,調用 afterPropertiesSet() 方法 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 的 postProcessAfterInitialization 回調 wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName); } return wrappedBean; }
完成刷新,初始化LifecycleProcessor,發佈容器初始化完成的事件
protected void finishRefresh() { // Clear context-level resource caches (such as ASM metadata from scanning). clearResourceCaches(); // Initialize lifecycle processor for this context. initLifecycleProcessor(); // Propagate refresh to lifecycle processor first. getLifecycleProcessor().onRefresh(); // Publish the final event. publishEvent(new ContextRefreshedEvent(this)); // Participate in LiveBeansView MBean, if active. LiveBeansView.registerApplicationContext(this); }
至此,SpringIOC容器的初始化過程就完成了,整個過程仍是很是複雜的。本文着重分析了整個邏輯過程,但願對你對IOC初始化的理解能有所幫助。