Spring系列——延迟加载

本文探讨了Spring中延迟加载的实现,从获取新鲜BeanFactory开始,详细讲解了Bean定义资源的加载和注册过程。重点分析了如何通过finishBeanFactoryInitialization方法预实例化非懒加载Bean,并展示了ConfigurableListableBeanFactory的preInstantiateSingletons方法在实例化Bean中的作用。了解这些,可以帮助我们理解当设置lazy-init=false时,Spring如何在注册后立即初始化Bean。
摘要由CSDN通过智能技术生成

Spring系列——延迟加载


Spring容器创建之后,会调用它的refresh方法刷新Spring应用的上下文。
首先整体查看AbstractApplicationContext#refresh源码

@Override
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 init
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值