Spring Boot 使用系列三(ConfigurableApplicationContext的refresh流程)

本文详细探讨了Spring Boot启动过程中ConfigurableApplicationContext的refresh()方法,包括线程安全、BeanFactory的准备、BeanFactoryPostProcessor与BeanPostProcessor的注册、MessageSource与ApplicationEventMulticaster初始化、ApplicationListener的注册以及单例bean的创建等关键步骤。通过这个流程,我们可以更深入理解Spring Boot的内部工作机制。
摘要由CSDN通过智能技术生成

接上一篇文章spring boot 使用系列二(启动流程)我们对spring boot的SpringApplication的初始化和run方法进行了分析,也对spring boot的启动过程有了整体的了解,但是我们遗留了一个,就是其中的refresh流程。今天我们来分析启动过程中的ConfigurableApplicationContext.refresh()流程,话不多说直接上代码:

使用startupShutdownMonitor保证线程安全

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()<
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值