Spring源碼解讀『IOC容器2-Bean加載過程』

上篇文章,我們自定義實現了一個簡單地IOC容器。本篇文章我們來介紹一下Spring IOC容器的實現。

1. 準備工作

爲了學習Spring的源碼實現,我們需要準備Spring的源碼環境,這時我們一般可以有以下兩種選擇:

1.1 下載spring-framework git項目

spring-framework git項目下載到本地

git clone https://github.com/spring-projects/spring-framework.git

將源碼導入到Idea中

這種方式存在一個問題,現在github下載速度非常慢,spring-framework又非常大,所以一般一般都會下載失敗。解決的辦法可以修改host文件或掛個代理,我是通過掛代理的方式既覺得,具體參考:https://www.zhihu.com/question/27159393

將源碼導入到Idea中後,Idea會自動Sync項目,等Sync結束(這個過程一般會挺久),表示spring-framework正確導入到本地了,這時候項目結構會是下圖這個樣子:

1.2 通過maven引入相關jar包

除了上述方式,我們還可以通過maven,將spring的相關jar包引入到本地項目中。比如我們再項目的pom.xml文件中添加spring-context的依賴:

<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.2.4.RELEASE</version>
</dependency>

然後就可以將spring-context相關包導入到項目中,但是這時候Idea默認只是導入了字節碼jar包,並沒有下載sourceCode包,對我們查看源碼非常不方便,我們可以通過如下設置,讓Idea在下載jar包的同時,下載sourceCode包。

Preferences > Build,Execution,Deployment > Build Tools > Maven > Importing,設置下載sourceCode包和javaDoc:

 

Idea重新導入下載源碼包:

2. 源碼入口

當我們試圖研究Spring源碼時,一般最開始總是無從下手的,因爲Spring整體層級結構、實現還是比較複雜的。並且平時我們使用時一般只會關心到配置文件(配置類)層面,很少去關注底層的實現。首先我們來看一下Spring IOC源碼閱讀的入口,我們再學習Spring時,肯定會接觸如下一段demo(這裏我們使用xml配置文件的demo出發,Java配置的Spring道理是一樣的,只不過入口類爲AnnotationConfigApplicationContext,將Bean配置加載爲BeanDefinition的過程不同,後續過程都是完全一致的):

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-context.xml");
applicationContext.getBean(XXX.class);

ClassPathXmlApplicationContext用於加載CLASSPATH下的Spring配置文件,可以看到,第二行就已經可以獲取到Bean的實例了,那麼必然第一行就已經完成了對所有Bean實例的加載(這裏完成加載,表示配置文件中定義的Bean已經實例化並初始化了)。那麼入口肯定在ClassPathXmlApplicationContext類的構造函數中,如下:

public ClassPathXmlApplicationContext(String configLocation) throws BeansException {
	this(new String[] {configLocation}, true, null);
}

在該構造方法中,調用了另一個構造方法,如下:

public ClassPathXmlApplicationContext(
		String[] configLocations, boolean refresh, @Nullable ApplicationContext parent)
		throws BeansException {

	super(parent);
	setConfigLocations(configLocations);
	if (refresh) {
		refresh();
	}
}
  • super方法:調用父類構造函數,這裏parent爲null,表示無父容器
  • setConfigLocations方法:用於將參數configLocations指定的Spring配置文件路徑中的${PlaceHolder}佔位符,替換爲系統變量中PlaceHolder對應的Value值,並存儲到成員變量configLocations中,方便我們後續將Bean定義加載爲BeanDefinition前,獲取配置文件的字節輸入流
  • refresh方法:Spring Bean加載的核心方法,它是ClassPathXmlApplicationContext的父類AbstractApplicationContext的一個方法,用於刷新Spring容器上下文信息,定義了Spring容器Bean加載的流程
@Override
public void refresh() throws BeansException, IllegalStateException {
	synchronized (this.startupShutdownMonitor) {
		// 1. 準備刷新Spring上下文,主要用來記錄Spring上下文加載開始時間,設置一些基礎成員變量value
		prepareRefresh();

		// 2. 刷新BeanFactory,此方法內完成配置文件中配置的Bean到BeanDefinition的轉化及註冊
		ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

		// 3. 準備Bean工廠,主要用來配置BeanFactory的基礎信息,例如上下文的ClassLoader和後處理器
		prepareBeanFactory(beanFactory);

		try {
			// 4. 允許子context添加一些BeanFactoryPostProcessor,
			// 比如Web應用中AbstractRefreshableWebApplicationContext添加ServletContextAwareProcessor,
			// 可以暫時略過這個方法
			postProcessBeanFactory(beanFactory);

			// 5. 執行BeanFactoryPostProcessor中定義的方法
			invokeBeanFactoryPostProcessors(beanFactory);

			// 6. 註冊所有的BeanPostProcessor,這部分BeanPostProcessor會在下面finishBeanFactoryInitialization方法
			// 過程中使用
			registerBeanPostProcessors(beanFactory);

			// 7. 初始化MessageSource,MessageSource是Spring定義的用於實現訪問國際化的接口
			initMessageSource();

			// 8. 初始化上下文事件廣播器
			initApplicationEventMulticaster();

			// 9. 模板方法,可以通過重寫它添加特殊上下文刷新的工作
			onRefresh();

			// 10. 註冊監聽器
			registerListeners();

			// 11. 實例化所有定義的單例Bean
			finishBeanFactoryInitialization(beanFactory);

			// 結束Spring上下文刷新
			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();
		}
	}
}

上述流程會在後面的文章詳細介紹,這裏只簡單羅列一下Bean加載的步驟。對於我們理解Bean加載比較重要的就是第2步和第11步,這兩步定義了Spring啓動時,Bean是如何從配置文件變成可使用的對象的過程。後面的文章會先介紹這一部分,再逐步講解剩餘的步驟。同時對於refresh方法我們還需要關注以下兩點:

  • refresh方法主體是加了同步鎖的,該鎖在關閉方法close中使用,就是爲了保證在調用refresh()方法的時候無法調用close()方法。或者調用close方法時無法調用refresh()方法,保證互斥性。
  • 該方法中,定義了跟Bean加載相關的很多子方法,每個子方法完成其獨立的功能,閱讀起來非常清晰明瞭。否則的花將散佈在各個子方法中的邏輯統一搬到refresh方法中,refresh方法至少要有幾千行了。所以通過模塊劃分明的子方法提高了代碼的可擴展性和可維護性。

3. 繼承體系

通過上述簡單地邏輯梳理,我們可以發現,在Bean加載過程中,比較重要的兩個類。分別爲入口類ClassPathXmlApplicationContext,以及該入口類通過繼承得到的成員變量beanFactory,代碼中可以發現該實例爲DefaultListableBeanFactory。我們來看一下這兩個類的繼承體系。

 

類比我們之前自定義實現的IOC容器,可以發現ClassPathXmlApplicationContext的功能跟我們之前自定義實現的功能基本一致,實現了BeanFactory接口,所以是這一種特殊的BeanFactory。對於ClassPathXmlApplicationContext特別需要需要注意的是,其繼承的抽象類AbstractRefreshableApplicationContext有一個DefaultListableBeanFactory實例,ClassPathXmlApplicationContext類能夠實現Bean工廠的功能都是通過繼承的該成員變量實現的。該成員變量會在上述refresh方法的子方法obtainFreshBeanFactory中初始化

對於DefaultListableBeanFactory,我們需要重點關注的是,該類同時實現了BeanFactory接口和BeanDefinitionRegistry接口,是Bean工廠也是Bean註冊中心

4. Bean加載流程

下面我們來看一下Bean加載流程,其實也就是refresh方法的大致執行流程。

4.1 prepareRefresh

該方法是AbstractApplicationContext的方法,如下:

/**
* Prepare this context for refreshing, setting its startup date and
* active flag as well as performing any initialization of property sources.
*/
protected void prepareRefresh() {
	this.startupDate = System.currentTimeMillis();
	this.closed.set(false);
	this.active.set(true);

	if (logger.isInfoEnabled()) {
		logger.info("Refreshing " + this);
	}

	// Initialize any placeholder property sources in the context environment
	initPropertySources();

	// Validate that all properties marked as required are resolvable
	// see ConfigurablePropertyResolver#setRequiredProperties
	getEnvironment().validateRequiredProperties();

	// Allow for the collection of early ApplicationEvents,
	// to be published once the multicaster is available...
	this.earlyApplicationEvents = new LinkedHashSet<>();
}

從方法註釋就可以看到,該方法的功能是爲刷新上下文做準備,設置Spring上下文加載開始時間。其餘就是設置一些成員變量value,比如closed標誌、active標誌。很簡單,也沒有什麼特別要講的。

4.2 obtainFreshBeanFactory

該方法對我們理解Spring Bean加載過程比較重要,需要重點關注。該方法是AbstractApplicationContext的方法,作用是獲取刷新Spring上下文的Bean工廠,該方法的返回值就是上面講的AbstractRefreshableApplicationContext類的DefaultListableBeanFactory類型成員變量。並且在該方法中,使用XMLBeanDefinitionReader完成了xml配置文件到BeanDefinition的轉換,如下:

protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
	refreshBeanFactory();
	ConfigurableListableBeanFactory beanFactory = getBeanFactory();
	if (logger.isDebugEnabled()) {
		logger.debug("Bean factory for " + getDisplayName() + ": " + beanFactory);
	}
	return beanFactory;
}

該方法最終返回的對象是通過getBeanFactory方法獲取的,而getBeanFactory方法在AbstractApplicationContext類中是個抽象方法:

public abstract ConfigurableListableBeanFactory getBeanFactory() throws IllegalStateException;

具體實現在子類AbstractRefreshableApplicationContext中,如下:

@Override
public final ConfigurableListableBeanFactory getBeanFactory() {
	synchronized (this.beanFactoryMonitor) {
		if (this.beanFactory == null) {
			throw new IllegalStateException("BeanFactory not initialized or already closed - " +
					"call 'refresh' before accessing beans via the ApplicationContext");
		}
		return this.beanFactory;
	}
}

方法實現其實其實就是返回AbstractRefreshableApplicationContext的成員變量實例。所以在上述調用getBeanFactory()方法之前,肯定完成了AbstractRefreshableApplicationContext成員變量beanFactory的初始化。來看一下調用getBeanFactory方法之前調用的refreshBeanFactory方法:

protected abstract void refreshBeanFactory() throws BeansException, IllegalStateException;

也是AbstractApplicationContext類的抽象方法,具體實現在子類AbstractRefreshableApplicationContext:

@Override
protected final void refreshBeanFactory() throws BeansException {
	if (hasBeanFactory()) {
		destroyBeans();
		closeBeanFactory();
	}
	try {
		//1. 創建DefaultListableBeanFactory對象實例
		DefaultListableBeanFactory beanFactory = createBeanFactory();
		//2. 設置serializationId
		beanFactory.setSerializationId(getId());
		//3. 定製化設置beanFactory
		customizeBeanFactory(beanFactory);
		//4. 加載xml配置文件中的Bean配置,轉化爲BeanDefinition,並註冊到註冊中心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);
	}
}

4.3 prepareBeanFactory

prepareBeanFactory方法是AbstractApplicationContext類的方法,用於配置BeanFactory的基本特性:

/**
* 配置BeanFactory的基本特性,比如ClassLoader、post-processor後置處理器
*/
protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
	// 1. 爲beanFactory配置classLoader
	beanFactory.setBeanClassLoader(getClassLoader());
	// 2. 爲beanFactory設置spEl表達是語言處理器,用來解析#{bean.xxx}方式設置的屬性值
	beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));
	// 3. 爲beanFactory設置屬性編輯器
	beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));

	// 4. 爲beanFactory添加ApplicationContextAwareProcessor用於上下文回調
	beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));

	// 5. 設置下面這些接口的實現類Bean不會被自動裝配
	beanFactory.ignoreDependencyInterface(EnvironmentAware.class);
	beanFactory.ignoreDependencyInterface(EmbeddedValueResolverAware.class);
	beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);
	beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);
	beanFactory.ignoreDependencyInterface(MessageSourceAware.class);
	beanFactory.ignoreDependencyInterface(ApplicationContextAware.class);

	// 6. 修正依賴,設置自動裝配的特殊規則,比如是BeanFactory接口的實現類,則修正爲當前BeanFactory
	beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory);
	beanFactory.registerResolvableDependency(ResourceLoader.class, this);
	beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this);
	beanFactory.registerResolvableDependency(ApplicationContext.class, this);

	// 7. 爲beanFactory添加ApplicationListenerDetector後置處理器,用於將實現了ApplicationListener接口的bean註冊爲監聽器
	beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));

	// 8. 如果自定義的Bean中有定義過名爲"loadTimeWeaver"的Bean,則會添加一個LoadTimeWeaverAwareProcessor
	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()));
	}

	// 9. 爲beanFactory註冊系統環境信息bean,比如"environment"、"systemProperties"、"systemEnvironment"
	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());
	}
}

4.4 invokeBeanFactoryPostProcessors

invokeBeanFactoryPostProcessors是AbstractApplicationContext的方法,用於執行實現了BeanFactoryPostProcessor中定義的方法。是Spring留給用戶的一個非常有用的擴展點,BeanFactoryPostProcessor接口提供了所有Bean實例化前的操作的擴展點。同時需要注意的是,這裏講的Bean實例化前,表示的是調用Bean的初始化方法前,BeanFactoryPostProcessor接口方法調用時機是任意一個自定義的Bean被實例化前,且只會被執行一次。

protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) {
	// 反射執行BeanFactoryPostProcessor所有實現類的方法
	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()));
	}
}
public static void invokeBeanFactoryPostProcessors(
		ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) {

	// Invoke BeanDefinitionRegistryPostProcessors first, if any.
	Set<String> processedBeans = new HashSet<>();

	
	if (beanFactory instanceof BeanDefinitionRegistry) {
		BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
		List<BeanFactoryPostProcessor> regularPostProcessors = new ArrayList<>();
		List<BeanDefinitionRegistryPostProcessor> registryProcessors = new ArrayList<>();

		// 1. BeanDefinitionRegistryPostProcessor接口繼承了BeanFactoryPostProcessor接口,
		// 這裏執行BeanDefinitionRegistryPostProcessor接口的postProcessBeanDefinitionRegistry方法
		// beanFactoryPostProcessors參數是通過getBeanFactoryPostProcessors()獲取的,
		// 通過xml配置文件啓動Spring容器時,該方法返回爲空,所以不會進for循環
		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<>();

		// 2. 收集所有實現了PriorityOrdered接口的BeanDefinitionRegistryPostProcessor,並執行postProcessBeanDefinitionRegistry方法
		String[] postProcessorNames =
				beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
		for (String ppName : postProcessorNames) {
			if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
				// currentRegistryProcessors用於存儲當前正在處理的某種類型的BeanDefinitionRegistryPostProcessor集合
				currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
				// processedBeans用於存儲已經註冊過的BeanDefinitionRegistryPostProcessor名稱,用於下面排除實現了PriorityOrdered接口
				// 和實現了Ordered接口的BeanDefinitionRegistryPostProcessor
				processedBeans.add(ppName);
			}
		}
		//排序
		sortPostProcessors(currentRegistryProcessors, beanFactory);
		// 將當前類型的BeanDefinitionRegistryPostProcessor添加到已註冊容器
		// 用於下面執行BeanDefinitionRegistryPostProcessor的postProcessBeanFactory方法
		registryProcessors.addAll(currentRegistryProcessors);
		// 執行BeanDefinitionRegistryPostProcessor的postProcessBeanDefinitionRegistry方法
		invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
		// 將currentRegistryProcessors容器清空,該容器會繼續處理其他類型的BeanDefinitionRegistryPostProcessor
		currentRegistryProcessors.clear();

		// 3. 收集所有實現了Ordered接口的BeanDefinitionRegistryPostProcessor,並執行postProcessBeanDefinitionRegistry方法
		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();

		// 4. 收集除實現了PriorityOrdered接口和Ordered接口的BeanDefinitionRegistryPostProcessor,並執行postProcessBeanDefinitionRegistry方法
		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();
		}

		// 5. 執行BeanDefinitionRegistryPostProcessor的postProcessBeanFactory方法
		invokeBeanFactoryPostProcessors(registryProcessors, beanFactory);
		invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);
	}

	else {
		// Invoke factory processors registered with the context instance.
		invokeBeanFactoryPostProcessors(beanFactoryPostProcessors, beanFactory);
	}

	// 6. 處理所有的BeanFactoryPostProcessor,邏輯跟上面處理BeanDefinitionRegistryPostProcessor類似
	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();
}

4.5 registerBeanPostProcessors

registerBeanPostProcessors方法是AbstractApplicationContext類中定義的方法,用於註冊BeanPostProcessor。BeanPostProcessor提供了每個Bean初始化前後操作的擴展點。注意registerBeanPostProcessors方法是用於註冊BeanPostProcessor,並沒有真正執行BeanPostProcessor中定義的前置和後置方法

/**
* Instantiate and invoke all registered BeanPostProcessor beans,
* respecting explicit order if given.
* <p>Must be called before any instantiation of application beans.
*/
protected void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory) {
	PostProcessorRegistrationDelegate.registerBeanPostProcessors(beanFactory, this);
}
public static void registerBeanPostProcessors(
		ConfigurableListableBeanFactory beanFactory, AbstractApplicationContext applicationContext) {

	// 1. 獲取所有的BeanPostProcessor類型的BeanName
	String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanPostProcessor.class, true, false);

	// Register BeanPostProcessorChecker that logs an info message when
	// a bean is created during BeanPostProcessor instantiation, i.e. when
	// a bean is not eligible for getting processed by all BeanPostProcessors.
	int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 + postProcessorNames.length;
	beanFactory.addBeanPostProcessor(new BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount));

	// 2. 將所有的BeanPostProcessor分爲實現了PriorityOrdered接口、實現了Ordered接口和其它三類
	List<BeanPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
	List<BeanPostProcessor> internalPostProcessors = new ArrayList<>();
	List<String> orderedPostProcessorNames = new ArrayList<>();
	List<String> nonOrderedPostProcessorNames = new ArrayList<>();
	for (String ppName : postProcessorNames) {
		if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
			BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
			priorityOrderedPostProcessors.add(pp);
			if (pp instanceof MergedBeanDefinitionPostProcessor) {
				internalPostProcessors.add(pp);
			}
		}
		else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
			orderedPostProcessorNames.add(ppName);
		}
		else {
			nonOrderedPostProcessorNames.add(ppName);
		}
	}

	// 3. 排序後註冊所有實現了PriorityOrdered接口的BeanPostProcessor
	// 這裏的註冊,是將BeanPostProcessor添加到beanFactory的成員變量beanPostProcessors
	sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
	registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors);

	// 4. 排序後註冊所有實現了Ordered接口的BeanPostProcessor
	List<BeanPostProcessor> orderedPostProcessors = new ArrayList<>();
	for (String ppName : orderedPostProcessorNames) {
		BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
		orderedPostProcessors.add(pp);
		if (pp instanceof MergedBeanDefinitionPostProcessor) {
			internalPostProcessors.add(pp);
		}
	}
	sortPostProcessors(orderedPostProcessors, beanFactory);
	registerBeanPostProcessors(beanFactory, orderedPostProcessors);

	// 5. 註冊所有除上述兩種BeanPostProcessor之外的BeanPostProcessor
	List<BeanPostProcessor> nonOrderedPostProcessors = new ArrayList<>();
	for (String ppName : nonOrderedPostProcessorNames) {
		BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
		nonOrderedPostProcessors.add(pp);
		if (pp instanceof MergedBeanDefinitionPostProcessor) {
			internalPostProcessors.add(pp);
		}
	}
	registerBeanPostProcessors(beanFactory, nonOrderedPostProcessors);

	// 6. 排序後註冊所有MergedBeanDefinitionPostProcessor實例
	sortPostProcessors(internalPostProcessors, beanFactory);
	registerBeanPostProcessors(beanFactory, internalPostProcessors);

	// 7. 註冊ApplicationListenerDetector
	beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext));
}

4.6 initMessageSource

initMessageSource方法是AbstractApplicationContext類中定義的方法,用於初始化MessageSource,MessageSource是Spring定義的用於實現訪問國際化的接口。

/**
* Initialize the MessageSource.
* Use parent's if none defined in this context.
*/
protected void initMessageSource() {
	ConfigurableListableBeanFactory beanFactory = getBeanFactory();
	// 如果自定義了名爲"messageSource"的Bean,就實例化該bean
	if (beanFactory.containsLocalBean(MESSAGE_SOURCE_BEAN_NAME)) {
		this.messageSource = beanFactory.getBean(MESSAGE_SOURCE_BEAN_NAME, MessageSource.class);
		// Make MessageSource aware of parent MessageSource.
		if (this.parent != null && this.messageSource instanceof HierarchicalMessageSource) {
			HierarchicalMessageSource hms = (HierarchicalMessageSource) this.messageSource;
			if (hms.getParentMessageSource() == null) {
				// Only set parent context as parent MessageSource if no parent MessageSource
				// registered already.
				hms.setParentMessageSource(getInternalParentMessageSource());
			}
		}
		if (logger.isDebugEnabled()) {
			logger.debug("Using MessageSource [" + this.messageSource + "]");
		}
	}
	// 否則,默認生成一個DelegatingMessageSource實例,並註冊到beanFactory
	else {
		// Use empty MessageSource to be able to accept getMessage calls.
		DelegatingMessageSource dms = new DelegatingMessageSource();
		dms.setParentMessageSource(getInternalParentMessageSource());
		this.messageSource = dms;
		beanFactory.registerSingleton(MESSAGE_SOURCE_BEAN_NAME, this.messageSource);
		if (logger.isDebugEnabled()) {
			logger.debug("Unable to locate MessageSource with name '" + MESSAGE_SOURCE_BEAN_NAME +
					"': using default [" + this.messageSource + "]");
		}
	}
}

4.7 initApplicationEventMulticaster

initApplicationEventMulticaster方法是AbstractApplicationContext類中定義的方法,用於初始化Spring容器廣播器。

/**
* Initialize the ApplicationEventMulticaster.
* Uses SimpleApplicationEventMulticaster if none defined in the context.
* @see org.springframework.context.event.SimpleApplicationEventMulticaster
*/
protected void initApplicationEventMulticaster() {
	ConfigurableListableBeanFactory beanFactory = getBeanFactory();
	// 如果自定義了名爲"applicationEventMulticaster"的Bean,就實例化該Bean,註冊到容器
	if (beanFactory.containsLocalBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME)) {
		this.applicationEventMulticaster =
				beanFactory.getBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, ApplicationEventMulticaster.class);
		if (logger.isDebugEnabled()) {
			logger.debug("Using ApplicationEventMulticaster [" + this.applicationEventMulticaster + "]");
		}
	}
	// 否則生成一個SimpleApplicationEventMulticaster實例,註冊到容器
	else {
		this.applicationEventMulticaster = new SimpleApplicationEventMulticaster(beanFactory);
		beanFactory.registerSingleton(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, this.applicationEventMulticaster);
		if (logger.isDebugEnabled()) {
			logger.debug("Unable to locate ApplicationEventMulticaster with name '" +
					APPLICATION_EVENT_MULTICASTER_BEAN_NAME +
					"': using default [" + this.applicationEventMulticaster + "]");
		}
	}
}

4.8 onRefresh

onRefresh方法是AbstractApplicationContext類中定義的方法,該方法是一個模板方法。重寫該方法,可以爲容器添加特殊上下文刷新的工作,在特殊Bean的初始化時、初始化之前被調用。比如AbstractRefreshableWebApplicationContext、StaticWebApplicationContext都重寫了該方法。

/**
* Template method which can be overridden to add context-specific refresh work.
* Called on initialization of special beans, before instantiation of singletons.
* <p>This implementation is empty.
* @throws BeansException in case of errors
* @see #refresh()
*/
protected void onRefresh() throws BeansException {
	// For subclasses: do nothing by default.
}

4.9 registerListeners

registerListeners方法是AbstractApplicationContext類中定義的方法,用於註冊監聽器。

/**
* Add beans that implement ApplicationListener as listeners.
* Doesn't affect other listeners, which can be added without being beans.
*/
protected void registerListeners() {
	// Register statically specified listeners first.
	for (ApplicationListener<?> listener : getApplicationListeners()) {
		getApplicationEventMulticaster().addApplicationListener(listener);
	}

	// Do not initialize FactoryBeans here: We need to leave all regular beans
	// uninitialized to let post-processors apply to them!
	String[] listenerBeanNames = getBeanNamesForType(ApplicationListener.class, true, false);
	for (String listenerBeanName : listenerBeanNames) {
		getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName);
	}

	// Publish early application events now that we finally have a multicaster...
	Set<ApplicationEvent> earlyEventsToProcess = this.earlyApplicationEvents;
	this.earlyApplicationEvents = null;
	if (earlyEventsToProcess != null) {
		for (ApplicationEvent earlyEvent : earlyEventsToProcess) {
			getApplicationEventMulticaster().multicastEvent(earlyEvent);
		}
	}
}

4.10 finishBeanFactoryInitialization

finishBeanFactoryInitialization方法是Spring容器Bean加載流程中最重要的一步,所有的非懶加載的Bean都會在該方法中實例化並初始化,這個方法我們再接下來的文章中單獨介紹。

4.11 finishRefresh

finishRefresh方法是AbstractApplicationContext類中定義的方法,用於結束Spring上下文刷新,對我們理解Spring容器Bean加載不是那麼在“重要”,可以選擇略過,這裏也不深入探討了。

/**
* Finish the refresh of this context, invoking the LifecycleProcessor's
* onRefresh() method and publishing the
* {@link org.springframework.context.event.ContextRefreshedEvent}.
*/
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);
}

參考鏈接:

1. Spring源碼

2. 非懶加載的單例Bean初始化前後的一些操作

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章