17-Spring源码解析之Bean的生命周期(2)——【getSingleton】和【createBean】

Spring版本:<version>5.2.1.RELEASE</version>

上一篇:16-Spring源码解析之Bean的生命周期(1)——doGetBean

上一篇我们介绍了doGetBean包含的功能,在【功能八】中,doGetBean会根据Beanscope类型进行Bean的加载。因为当前是Spring容器的创建,因此程序走到这一步的时候,创建的Beansingleton类型的。我再贴一下doGetBean中【功能八】的代码:

	if (mbd.isSingleton()) {
   
		sharedInstance = getSingleton(beanName, () -> {
   
			try {
   
				return createBean(beanName, mbd, args);
			}
			catch (BeansException ex) {
   
				// Explicitly remove instance from singleton cache: It might have been put there
				// eagerly by the creation process, to allow for circular reference resolution.
				// Also remove any beans that received a temporary reference to the bean.
				destroySingleton(beanName);
				throw ex;
			}
		});
		bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
	}

那么我们就进入getSingleton方法中一探究竟吧。

一、getSingleton获取单例

在上一篇文章的2.3.2节也调用了getSingleton方法,只是那时候是从缓存中获取单例,那么如果缓存中不存在当前的Bean,我们就需要从头开始加载Bean了,而Spring就是使用了getSingleton方法实现Bean的加载的。记得上一篇文章2.3.2节中提到的getSingleton的几种重载方法吗?忘记了不要紧,我再在这里贴一下:

在这里插入图片描述

上一篇在缓存中获取单例的时候,调用的两个getSingleton方法分别是:

  • public Object getSingleton(String beanName)方法
  • protected Object getSingleton(String beanName, boolean allowEarlyReference)方法

而我们从头开始加载Bean调用的getSingleton为:

  • public Object getSingleton(String beanName, ObjectFactory<?> singletonFactory)

接下来,我们就要看一下Spring是如何通过getSingleton来创建Bean的了,我们进入getSingleton方法

	public Object getSingleton(String beanName, ObjectFactory<?> singletonFactory) {
   
		Assert.notNull(beanName, "Bean name must not be null");
		synchronized (this.singletonObjects) {
   
//-----------------------------------【功能一】-----------------------------------
			// 又检查了一次缓存中是否已经加载过名字为beanName的Bean
			// 如果没有加载过,下面开始加载
			// 如果加载过,就直接返回缓存中该的Bean
			Object singletonObject = this.singletonObjects.get(beanName);


			if (singletonObject == null) {
   
				// 如果这个Bean正在被销毁,就抛异常
				if (this.singletonsCurrentlyInDestruction) {
   
					throw new BeanCreationNotAllowedException(beanName,
							"Singleton bean creation not allowed while singletons of this factory are in destruction " +
							"(Do not request a bean from a BeanFactory in a destroy method implementation!)");
				}
				if (logger.isDebugEnabled()) {
   
					logger.debug("Creating shared instance of singleton bean '" + beanName + "'");
				}


//-----------------------------------【功能二】--1.1 详细分析---------------------------------
				// 加载单例bean之前记录加载状态
				beforeSingletonCreation(beanName);
				boolean newSingleton = false;
				// 记录异常的异常链
				boolean recordSuppressedExceptions = (this.suppressedExceptions == null);
				if (recordSuppressedExceptions) {
   
					this.suppressedExceptions = new LinkedHashSet<>();
				}
//-----------------------------------【功能三】--1.4 详细分析---------------------------------
				try {
   
					// 创建Bean
					singletonObject = singletonFactory.getObject();
					// 设置标志位
					newSingleton = true;
				}
				catch (
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值