Spring容器启动过程详解(待完善)

public void refresh() throws BeansException, IllegalStateException {
		synchronized (this.startupShutdownMonitor) {
			StartupStep contextRefresh = this.applicationStartup.start("spring.context.refresh");
 
			// Prepare this context for refreshing.
			prepareRefresh();
 
			//1.创建或者获取新的BeanFactory
			//2.加载并注册Bean定义(a.从配置源加载Bean定义b.将解析后的BeanDefinition注册到BeanFactory的beanDefitionMap中)
			//3.返回BeanFactory()
			ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
 
			// 添加容器级别的后置处理器ApplicationContextAwareProcessor:负责处理Aware接口
			//Sping中的Aware接口如何定义,怎么用?
			prepareBeanFactory(beanFactory);
 
			try {
				// PostProcessBeanFactory是spring为ApplicationContext的子类提供的扩展点,为自定义ApplicationContext的上下文进行扩展的介入点
				postProcessBeanFactory(beanFactory);
 
				StartupStep beanPostProcess = this.applicationStartup.start("spring.context.beans.post-process");
				//1.执行BeanDefinitoinRegistryPostProcessor的实现类(方法postProcessBeanDefinitionRegistry),执行优先级PriorityOrdered>Order>普通处理器(PriorityOrdered和Order是接口),执行BeanFactoryPostProcessor的方法postProcessBeanFactory
				//2.执行BeanFactoryPostProcessor的实现类(方法postProcessBeanFactory),执行优先级PriorityOrdered>Order>普通处理器(PriorityOrdered和Order是接口)
				//3.BeanDefinitionRegistryPostProcessor extends BeanFactoryPostProcessor,
				//4.BeanDefinitionRegistryPostProcessor可以动态注册或者删除beanDefiniton,BeanFactoryPostProcessor只能修改beanDefinition的属性
				invokeBeanFactoryPostProcessors(beanFactory);
 
				// 从beanFactory的beanDefinitionMap中获取实现了BeanPostProcessor接口的类,按照优先级执行优先级PriorityOrdered>Order>普通处理器(PriorityOrdered和Order是接口)进行排序,将这些BeanPostProcessor接口的类放入到beanFactory中的beanPostProcessors中
				registerBeanPostProcessors(beanFactory);
				beanPostProcess.end();
 
				// 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();
				contextRefresh.end();
			}
		}
	}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值