spring学习记录(三)

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

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

spring学习过程中,applicationContext的refresh方法非常重要。这个方法中处理了spring容器的所有初始化的工作,包括初始化beanFactory、调用beanFactoryPostProcessor、初始化容器中的bean、处理国际化、处理spring监听器等,调用对应的方法来处理相应的操作。下面对这个方法中的步骤进行一下记录:
1、prepareRefresh() :此方法为给applicationContext中初始化一些属性:启动时间、关闭状态设置为false、活动状态设置为true、对properties等做相应校验、给listener等map进行初始化等。
2、obtainFreshBeanFactory():这个方法为获得一个新鲜的beanFactory,并为此beanFactory设置id等操作,经过仔细阅读源码,beanFactory在applicationContext初始化时就已经被实例化了:

public GenericApplicationContext() {
		this.beanFactory = new DefaultListableBeanFactory();
	}

3、prepareBeanFactory(beanFactory):此方法为准备使用beanFactory做一些准备工作,具体有:增加一些aware处理器,如ApplicationContextAwareProcessor等,相当于给beanFactory做一些初始化。
4、postProcessBeanFactory(beanFactory):此方法为扩展而设置,可以在子类中进行相应的实现,abstractApplicationContext中并未做实际处理。
5、invokeBeanFactoryPostProcessors(beanFactory):此方法为给beanFactory中添加相应的beanFactoryPostProcessor,说是添加,读源码时发现,其实beanFactory中并未赋值,在方法中,获取到相应的beanFactoryPostProcessor之后,直接进行了相应的invoke。
6、registerBeanPostProcessors(beanFactory):此方法为给beanFactory中注册所有的beanPostProcessor,beanPostProcessor在初始化bean前后调用相应的处理方法。
7、initMessageSource():此方法为处理i18n国际化。
8、initApplicationEventMulticaster():此方法初始化多播器。
9、onRefresh():此方法为扩展设置,可以在子类中进行相应实现。
10、registerListeners():注册监听器。
11、finishBeanFactoryInitialization(beanFactory):重要方法,初始化所有的非懒加载的单例bean。
12、finishRefresh():完成refresh方法,并且发布ContextRefreshEvent事件。

以上为自己学习中的理解,定有很多不足之处,不对的地方还请大家指正。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值