spring源码第一篇:总览refresh

本文详细剖析了Spring5.1版本中容器启动的refresh方法,涵盖12个核心步骤,包括准备刷新、创建beanFactory、初始化beanFactory、beanFactory后处理、注册beanPostProcessor、初始化消息源和事件多播器等,全面展示了Spring容器启动时的内部逻辑。
摘要由CSDN通过智能技术生成
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();
			}
		}
	}

以上是spring 5.1版本的容器启动refresh方法,一共12个方法,接下来我会每个方法挨个阅读,首先总览总体:

1、refresh前的准备工作

prepareRefresh()

启动时间,关闭标志,启动标志,初始化属性资源,获取标准环境对象并验证必须的属性,初始化应用监听器和事件集合

2、构建beanFactory

obtainFreshBeanFactory()

生成DefaultListableBeanFactory的bean工厂

3、工厂各种设置以及初始化

prepareBeanFactory(beanFactory)

包括设置bean的classloader,表达式解析器,忽略依赖接口,注册默认的environment等等

4、beanFactory增强器,扩展点

postProcessBeanFactory

5、调用增强方法

invokeBeanFactoryPostProcessors

6、注册bean的增强器

7、初始化消息源

8、初始化事件多播器

9、初始化其他bean,扩展点

onRefresh

10、注册监听器

11、完成bean工厂初始化

12、发布相应事件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值