深入理解Spring IOC原始碼分析

女友在高考發表於2021-11-16

Spring容器初始化

本文使用的是Spring 5.1.7版本

寫在前面:我們看原始碼一般有3種方式。

  • 第一種直接用class檔案,IDEA會幫我們反編譯成看得懂的java程式碼
  • 第二種是用maven的download Sources
  • 第三種是直接下載原始碼編譯

如果隨便看看,那麼第二種就行了。如果想仔細研究,可以選第三種,這樣可以給程式碼加註釋,或者修改一些東西等等。

Spring原始碼構建

  • 下載原始碼(github)
  • 安裝gradle
  • 匯入專案
  • 編譯工程(順序:core->xom->context->beans->aspects->aop)
  • 編譯方法:工程->tasks->compileTestJava

  • 編譯完成後,新建一個測試module,用gradle的方式,build.gradle為:
group 'org.springframework'
version '5.1.21.BUILD-SNAPSHOT'

apply plugin: 'java'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    compile(project(":spring-context"))
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

如果執行報錯,需要設定IDEA。
需要將Preferences > Build, Execution, Deployment > Build Tools > Gradle > Runner裡的 Delegate IDE build/run actions to gradle勾選上。


Spring IOC容器初始化的關鍵環節就在AbstractApplicationContext的refresh方法中。

public void refresh() throws BeansException, IllegalStateException {
		synchronized (this.startupShutdownMonitor) {
			//第一步:重新整理前的預處理
			prepareRefresh();

			// 第二步:獲取BeanFactory;載入BeanDefition並註冊到BeanDefitionRegistry
			ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

			// 第三步:BeanFactory的預準備工作
			prepareBeanFactory(beanFactory);

			try {
				// 第四步:BeanFactory準備工作完成之後進行的後置處理
				postProcessBeanFactory(beanFactory);

				// 第五步:例項化並呼叫實現了BeanFactoryPostProcessor介面的Bean
				invokeBeanFactoryPostProcessors(beanFactory);

				// 第六步:註冊BeanPostProcessor,在建立bean的前後等執行。
				registerBeanPostProcessors(beanFactory);

				// 第七步:初始化MessageSource元件(做國際化功能;訊息繫結,訊息解析)
				initMessageSource();

				// 第八步:初始化事件派發器
				initApplicationEventMulticaster();

				//第九步:提供給子類重寫,容器重新整理的適合可以自定義邏輯
				onRefresh();

				// 第十步:註冊應用監聽器。就是註冊實現了ApplicationListener介面的監聽器bean
				registerListeners();

				// 第十一步:初始化所有剩下的非懶載入的單例bean,
				//填充屬性
				//初始化方法呼叫
				//呼叫BeanPostProcessor對bean進行後置處理
				finishBeanFactoryInitialization(beanFactory);

				// 第十二步:完成context的重新整理。主要是呼叫LifecycleProcessor的onRefresh方法,並且釋出ContextRefreshedEvent事件
				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();
			}
		}
	}

接下來我們分析第二步中的兩個子流程:

2.1. 獲取BeanFactory子流程

時序圖如下:

2.2. BeanDefinition載入解析及註冊⼦流程

BeanDefinition載入流程入口在AbstractRefreshableApplicationContext#refreshBeanFactory ⽅法中,會呼叫到loadBeanDefinitions方法,這個方法最終會由XmlBeanDefinitionReader的loadBeanDefinitions方法來完成。然後會呼叫它自己的doLoadBeanDefinitions方法。

protected int doLoadBeanDefinitions(InputSource inputSource, Resource resource)
			throws BeanDefinitionStoreException {

		try {
			//讀取xml資訊,將xml資訊儲存到Document中
			Document doc = doLoadDocument(inputSource, resource);
			//解析doc,封裝到BeanDefinition物件並進行註冊
			int count = registerBeanDefinitions(doc, resource);
			if (logger.isDebugEnabled()) {
				logger.debug("Loaded " + count + " bean definitions from " + resource);
			}
			return count;
		}
	//省略部分程式碼....

繼續跟蹤registerBeanDefinitions方法。

public int registerBeanDefinitions(Document doc, Resource resource) throws BeanDefinitionStoreException {
		BeanDefinitionDocumentReader documentReader = createBeanDefinitionDocumentReader();
		//獲取已有的BeanDefinition的數量
		int countBefore = getRegistry().getBeanDefinitionCount();
		//註冊BeanDefinitions
		documentReader.registerBeanDefinitions(doc, createReaderContext(resource));
		//返回新註冊的BeanDefinition的數量
		return getRegistry().getBeanDefinitionCount() - countBefore;
	}

它又交由documentReader來執行registerBeanDefinitions方法。documentReader接收後呼叫本類的doRegisterBeanDefinitions來執行。這個方法裡我們需要關注parseBeanDefinitions.preProcessXml和postProcessXml都是留給子類實現的。

protected void doRegisterBeanDefinitions(Element root) {
	    //省略部分程式碼...
		preProcessXml(root);
		parseBeanDefinitions(root, this.delegate);
		postProcessXml(root);

		this.delegate = parent;
	}

跟蹤parseBeanDefinitions方法,可以看到由DefaultBeanDefinitionDocumentReader#parseDefaultElement在進行解析

private void parseDefaultElement(Element ele, BeanDefinitionParserDelegate delegate) {
		//import處理
		if (delegate.nodeNameEquals(ele, IMPORT_ELEMENT)) {
			importBeanDefinitionResource(ele);
		}
		//alias處理
		else if (delegate.nodeNameEquals(ele, ALIAS_ELEMENT)) {
			processAliasRegistration(ele);
		}
		//解析bean元素
		else if (delegate.nodeNameEquals(ele, BEAN_ELEMENT)) {
			processBeanDefinition(ele, delegate);
		}
		//巢狀bean處理
		else if (delegate.nodeNameEquals(ele, NESTED_BEANS_ELEMENT)) {
			// recurse
			doRegisterBeanDefinitions(ele);
		}
	}

最後跟蹤processBeanDefinition的程式碼

protected void processBeanDefinition(Element ele, BeanDefinitionParserDelegate delegate) {
		//解析bean元素為BeanDefinition,但是此時使用BeanDefinitionHolder又包裝了一層
		BeanDefinitionHolder bdHolder = delegate.parseBeanDefinitionElement(ele);
		if (bdHolder != null) {
			bdHolder = delegate.decorateBeanDefinitionIfRequired(ele, bdHolder);
			try {
				// 完成BeanDefinition的註冊
				BeanDefinitionReaderUtils.registerBeanDefinition(bdHolder, getReaderContext().getRegistry());
			}
			catch (BeanDefinitionStoreException ex) {
				getReaderContext().error("Failed to register bean definition with name '" +
						bdHolder.getBeanName() + "'", ele, ex);
			}
			// Send registration event.
			getReaderContext().fireComponentRegistered(new BeanComponentDefinition(bdHolder));
		}
	}

到這裡就完成了BeanDefinition的註冊。我們發現,所謂的註冊就是把 XML 中定義的Bean資訊封裝為BeanDefinition物件之後放⼊⼀個Map中。

this.beanDefinitionMap.put(beanName, beanDefinition);

簡略的時序圖如下:

接下來我們分析一下Bean建立的流程。Bean建立是在refresh主流程中的第十一步裡面。

11.1 Bean建立子流程

我們進入finishBeanFactoryInitialization方法。

protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory) {
		//省略部分程式碼...
		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);

		// Allow for caching all bean definition metadata, not expecting further changes.
		beanFactory.freezeConfiguration();

		// 例項化所有非懶載入的bean
		beanFactory.preInstantiateSingletons();
	}

跟蹤preInstantiateSingletons方法

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.
		List<String> beanNames = new ArrayList<>(this.beanDefinitionNames);

		// 遍歷所有的beanName
		for (String beanName : beanNames) {
			RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
			if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {
				//如果是工廠bean,即實現了FactoryBean介面
				if (isFactoryBean(beanName)) {
					Object bean = getBean(FACTORY_BEAN_PREFIX + beanName);
					if (bean instanceof FactoryBean) {
						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);
				}
			}
		}
    //省略...
	}

我們發現不管是工廠Bean,還是普通Bean都會呼叫getBean(beanName);方法。然後呼叫到doGetBean方法。

protected <T> T doGetBean(
			String name, @Nullable Class<T> requiredType, @Nullable Object[] args, boolean typeCheckOnly)
			throws BeansException {

		String beanName = transformedBeanName(name);
		Object 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 {
			//如果已經有建立過程中的同beanName的bean,拋異常
			if (isPrototypeCurrentlyInCreation(beanName)) {
				throw new BeanCurrentlyInCreationException(beanName);
			}

			// 從parentBeanFactory裡找對應的bean
			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) {
				//打一個已建立的標記
				markBeanAsCreated(beanName);
			}

			try {
				RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
				checkMergedBeanDefinition(mbd, beanName, args);

				// 保證當前bean所依賴的bean的初始化
				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(dep);
						}
						catch (NoSuchBeanDefinitionException ex) {
							throw new BeanCreationException(mbd.getResourceDescription(), beanName,
									"'" + beanName + "' depends on missing bean '" + dep + "'", ex);
						}
					}
				}

				//建立bean
				// 單例bean
				if (mbd.isSingleton()) {
					sharedInstance = getSingleton(beanName, () -> {
						try {
							//具體的建立bean邏輯
							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);
				}
				//多例bean
				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 {
					String scopeName = mbd.getScope();
					if (!StringUtils.hasLength(scopeName)) {
						throw new IllegalStateException("No scope name defined for bean ´" + beanName + "'");
					}
					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;
			}
		}

		// 檢查所需型別是否與實際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.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;
	}

可以看到經過一些判斷和校驗,會調createBean方法去建立bean,然後呼叫doCreateBean去完成建立。

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) {
			//建立bean例項,但是尚未設定屬性
			instanceWrapper = createBeanInstance(beanName, mbd, args);
		}
		Object bean = instanceWrapper.getWrappedInstance();
		Class<?> beanType = instanceWrapper.getWrappedClass();
		if (beanType != NullBean.class) {
			mbd.resolvedTargetType = beanType;
		}

		//允許後處理器修改合併的bean定義
		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;
			}
		}

		//急切地快取單例,以便能夠解析迴圈引用
		//即使由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");
			}
			addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean));
		}

		// 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) {
			Object earlySingletonReference = getSingleton(beanName, false);
			if (earlySingletonReference != null) {
				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 " +
								"'getBeanNamesForType' 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;
	}

在上述的方法中會先例項化一個bean,然後注入property裡面配置的屬性,最後執行初始化相關方法。

簡單時序圖:

相關文章