Spring源码学习-IOC初始化过程-refresh()方法揭秘

前面两篇分别讲了Spring初始化容器的时候XML配置方式和注解方式如何解析注册BeanDefinition的整个过程。Spring在初始化 ApplicationContext容器的过程当中注册BeanDefinition只是其中的一个步骤,其中还有不少处理好比初始化国际化资源、应用事件广播、应用事件监听等都是在抽象类AbstractApplicationContext的refresh()方法中完成的。java

下面是整个refresh()方法的流程 app

1.prepareRefresh():容器刷新前的准备,设置了开始时间、状态、验证全部标记为必需的属性是否可解析和一个模板方法initPropertySources()。post

2.obtainFreshBeanFactory():里面两个模板方法refreshBeanFactory()和getBeanFactory()。看过以前xml配置方式注册BeanDefinition的应该知道入口就是AbstractRefreshableApplicationContext的refreshBeanFactory()。getBeanFactory()返回子类建立的bean factory实例,AbstractApplicationContext中不存在BeanFactory的实例,因此提供模板方法由子类具体建立提供。spa

3.prepareBeanFactory(beanFactory):容器使用前对bean工厂进行一些处理属性设置。rest

4.postProcessBeanFactory(beanFactory):模板方法提供给子类对bean工厂进行一些处理。code

5.invokeBeanFactoryPostProcessors(beanFactory):这是一个重点,也是一个扩展点,同时用java作配置时的扫描注册BeanDefinition的入口也在这里。cdn

首先初始化类型为BeanDefinitionRegistryPostProcessors而且实现了接口PriorityOrdered(高优先级)的bean并执行postProcessBeanDefinitionRegistry方法,ConfigurationClassPostProcessor就是在这里执行而后调用doscan方法扫描注册bean定义。xml

// 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<>();

	// First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered.
	String[] postProcessorNames =
			beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
	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(排序接口)的BeanDefinitionRegistryPostProcessors实例并调用postProcessBeanDefinitionRegistry方法blog

// Next, invoke the BeanDefinitionRegistryPostProcessors that implement Ordered.
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();
复制代码

最后实例化没有实现排序接口的BeanDefinitionRegistryPostProcessors实例并调用postProcessBeanDefinitionRegistry方法。 后面是实例实例化实现了BeanFactoryPostProcessor接口的bean并调用postProcessBeanFactory方法,调用顺序和BeanDefinitionRegistryPostProcessors同样。排序

在这里咱们能够插入本身的实现,经过实现BeanDefinitionRegistryPostProcessors接口对bean定义作一些处理,实现BeanFactoryPostProcessor接口能够对bean工厂作一些处理工做。

6.registerBeanPostProcessors(beanFactory):这也是一个扩展点,在这里会提早初始化全部实现了BeanPostProcessor接口的bean,但不会执行里面的方法。BeanPostProcessor 实例 在getBean()以前和以后能够作一些处理,AOP就是基于BeanPostProcessor实现的。 下面是源码实现

public static void registerBeanPostProcessors(
	ConfigurableListableBeanFactory beanFactory, AbstractApplicationContext applicationContext) {

	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));

	// Separate between BeanPostProcessors that implement PriorityOrdered,
	// Ordered, and the rest.
	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);
		}
	}

	// First, register the BeanPostProcessors that implement PriorityOrdered.
	sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
	registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors);

	// Next, register the BeanPostProcessors that implement Ordered.
	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);

	// Now, register all regular BeanPostProcessors.
	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);

	// Finally, re-register all internal BeanPostProcessors.
	sortPostProcessors(internalPostProcessors, beanFactory);
	registerBeanPostProcessors(beanFactory, internalPostProcessors);

	// Re-register post-processor for detecting inner beans as ApplicationListeners,
	// moving it to the end of the processor chain (for picking up proxies etc).
	beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext));
	    
}
复制代码

处理逻辑跟上面的BeanFactoryPostProcessor差很少,都是先PriorityOrdered,而后Ordered,最后是普通的实现。

7.initMessageSource():初始化国际化资源。

8.initApplicationEventMulticaster():初始化事件广播。

9.onRefresh();模板方法。

10.registerListeners():注册事件监听

11.finishBeanFactoryInitialization(beanFactory):初始化全部不是懒加载的单例bean。

12.finishRefresh():完成初始化以后的工做。

整个流程中最主要的其实就是步骤二、五、6和11。分别是xml配置加载Bean定义;初始化实现了BeanFactoryPostProcessor接口的实例,并执行对应的方法;初始化实现了BeanPostProcessor接口的实例;初始化全部非懒加载的单例bean。其余代码感兴趣的能够本身去了解,这里不作过多分析。

相关文章
相关标签/搜索