上一讲我们谈到单例生产关键方法getSingleton。getSingleton方法由DefaultSingletonBeanRegistry类实现。我们的抽象工厂AbstractBeanFactory继承了FactoryBeanRegistrySupport,而FactoryBeanRegistrySupport则继承了DefaultSingletonBeanRegistry,AbstractBeanFactory注册实例的工作实际上由DefaultSingletonBeanRegistry实施,不过其中生产单例的操作还是回调了AbstractBeanFactory中的createBean方法。

DefaultSingletonBeanRegistry类结构

可以看到,DefaultSingletonBeanRegistry实现了

(1)单例注册SingletonBeanRegistry

(2)别名注册SimpleAliasRegistry

同时,DefaultSingletonBeanRegistry需要传入ObjectFactory实现实例的创建,传入DisposableBean实现实例的销毁。

    /**
* Return the (raw) singleton object registered under the given name,
* creating and registering a new one if none registered yet.
* 返回一个单例,不存在则创建并注册
* @param beanName the name of the bean
* @param singletonFactory the ObjectFactory to lazily create the singleton
* 单例创建工厂
* with, if necessary
* @return the registered singleton object
*/
public Object getSingleton(String beanName, ObjectFactory<?> singletonFactory) {
Assert.notNull(beanName, "Bean name must not be null");
synchronized (this.singletonObjects) {
// 单例已经存在则直接返回
Object singletonObject = this.singletonObjects.get(beanName);
if (singletonObject == null) {
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 + "'");
}
// 单例创建前刷新状态
beforeSingletonCreation(beanName);
boolean newSingleton = false;
boolean recordSuppressedExceptions = (this.suppressedExceptions == null);
if (recordSuppressedExceptions) {
this.suppressedExceptions = new LinkedHashSet<>();
}
// 调用单例创建工厂创建单例
try {
singletonObject = singletonFactory.getObject();
newSingleton = true;
}
catch (IllegalStateException ex) {
// Has the singleton object implicitly appeared in the meantime ->
// if yes, proceed with it since the exception indicates that state.
singletonObject = this.singletonObjects.get(beanName);
if (singletonObject == null) {
throw ex;
}
}
catch (BeanCreationException ex) {
if (recordSuppressedExceptions) {
for (Exception suppressedException : this.suppressedExceptions) {
ex.addRelatedCause(suppressedException);
}
}
throw ex;
}
finally {
if (recordSuppressedExceptions) {
this.suppressedExceptions = null;
}
// 单例创建后刷新状态
afterSingletonCreation(beanName);
}
// 单例注册
if (newSingleton) {
addSingleton(beanName, singletonObject);
}
}
return singletonObject;
}
}

createBean方法我们在下一讲中研究。

Spring源码阅读(三)的更多相关文章

  1. Bean实例化(Spring源码阅读)-我们到底能走多远系列(33)

    我们到底能走多远系列(33) 扯淡: 各位:    命运就算颠沛流离   命运就算曲折离奇   命运就算恐吓着你做人没趣味   别流泪 心酸 更不应舍弃   ... 主题: Spring源码阅读还在继 ...

  2. Spring源码阅读-ApplicationContext体系结构分析

    目录 继承层次图概览 ConfigurableApplicationContext分析 AbstractApplicationContext GenericApplicationContext Gen ...

  3. Spring源码阅读笔记02:IOC基本概念

    上篇文章中我们介绍了准备Spring源码阅读环境的两种姿势,接下来,我们就要开始探寻这个著名框架背后的原理.Spring提供的最基本最底层的功能是bean容器,这其实是对IoC思想的应用,在学习Spr ...

  4. 25 BasicUsageEnvironment0基本使用环境基类——Live555源码阅读(三)UsageEnvironment

    25 BasicUsageEnvironment0基本使用环境基类——Live555源码阅读(三)UsageEnvironment 25 BasicUsageEnvironment0基本使用环境基类— ...

  5. 26 BasicUsageEnvironment基本使用环境——Live555源码阅读(三)UsageEnvironment

    26 BasicUsageEnvironment基本使用环境--Live555源码阅读(三)UsageEnvironment 26 BasicUsageEnvironment基本使用环境--Live5 ...

  6. 24 UsageEnvironment使用环境抽象基类——Live555源码阅读(三)UsageEnvironment

    24 UsageEnvironment使用环境抽象基类——Live555源码阅读(三)UsageEnvironment 24 UsageEnvironment使用环境抽象基类——Live555源码阅读 ...

  7. 初始化IoC容器(Spring源码阅读)

    初始化IoC容器(Spring源码阅读) 我们到底能走多远系列(31) 扯淡: 有个问题一直想问:各位你们的工资剩下来会怎么处理?已婚的,我知道工资永远都是不够的.未婚的你们,你们是怎么分配工资的? ...

  8. Sping学习笔记(一)----Spring源码阅读环境的搭建

    idea搭建spring源码阅读环境 安装gradle Github下载Spring源码 新建学习spring源码的项目 idea搭建spring源码阅读环境 安装gradle 在官网中下载gradl ...

  9. Spring源码阅读 之 配置的读取,解析

    在上文中我们已经知道了Spring如何从我们给定的位置加载到配置文件,并将文件包装成一个Resource对象.这篇文章我们将要探讨的就是,如何从这个Resouce对象中加载到我们的容器?加载到容器后又 ...

  10. 搭建 Spring 源码阅读环境

    前言 有一个Spring源码阅读环境是学习Spring的基础.笔者借鉴了网上很多搭建环境的方法,也尝试了很多,接下来总结两种个人认为比较简便实用的方法.读者可根据自己的需要自行选择. 方法一:搭建基础 ...

随机推荐

  1. 终于解决“Git Windows客户端保存用户名与密码”的问题(转载)

    add by zhj:不建议用这种方法,建议用SSH,参见 TortoiseGit密钥的配置 http://www.cnblogs.com/ajianbeyourself/p/3817364.html ...

  2. 自然语言处理nlp工具

    1.结巴 适合语言:python 应用场景:中文分词较好 不适用于命名实体识别.信息抽取 2.nltk 适合语言:python 应用场景:不适用于中文分词,效果较差 提供了一些用于方便的方法

  3. du 查看文件大小

    [root@localhost ~]# du -sh /etc # 查看目录下所有文件大小 [root@localhost ~]# du -sh * # 查看所有文件大小

  4. mysql show prifile基本详解

    show profile默认情况下,参数处于关闭状态,并保存最近15次的运行结果查看profile是否开启 show variables like '%profi%';开启profile记录功能 se ...

  5. Linux学习和ROS安装(1)

    参考文档:https://www.cnblogs.com/liu-fa/p/5779206.html#undefined 系统环境:Window7 64bit+VMware11 ubuntu-gnom ...

  6. EOS account 中的 Threshold 和 weight 使用

    https://eoscity.io/f/viewtopic.php?f=7&t=17 这篇文章的原文:   (https://steemit.com/eos/@genereos/eos-mu ...

  7. java中Long的比较

    Long的比较要用equals而不要用== 当Long为常量且常量值小于一个字节(<=127)时,两个Long指向同一个常量内容: Long userId=127L; Long authorId ...

  8. Java通过jxl读取Excel

    package com.hd.all.test.testjava; import java.io.File; import java.io.IOException; import java.util. ...

  9. mysql timestamp

     select from_unixtime(m.createdAt, '%Y-%m-%d %H:%i:%s') from kfrobotaidlog m; select m.customeruid,  ...

  10. python3 判断字符串是否为IP

    #!/usr/bin/python3 # -*- coding: utf-8 -*- import re ip = "192.168.1.1" ip = re.findall(&q ...