spring与设计模式之二单例模式
网络上都说ApplicationContext是单例,但看了原始代码,我认为应该是一个错误的表达。
我们来看Spring6.x中用springboot创建一个程序的时候默认的applicationContext是什么。
根据调试显示,这个ApplicationContext的实例是org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext的实例。

看看这个类的构造函数:
public class AnnotationConfigServletWebServerApplicationContext extends ServletWebServerApplicationContext
implements AnnotationConfigRegistry { private final AnnotatedBeanDefinitionReader reader; private final ClassPathBeanDefinitionScanner scanner; private final Set<Class<?>> annotatedClasses = new LinkedHashSet<>(); private String[] basePackages; /**
* Create a new {@link AnnotationConfigServletWebServerApplicationContext} that needs
* to be populated through {@link #register} calls and then manually
* {@linkplain #refresh refreshed}.
*/
public AnnotationConfigServletWebServerApplicationContext() {
this.reader = new AnnotatedBeanDefinitionReader(this);
this.scanner = new ClassPathBeanDefinitionScanner(this);
} /**
* Create a new {@link AnnotationConfigServletWebServerApplicationContext} with the
* given {@code DefaultListableBeanFactory}. The context needs to be populated through
* {@link #register} calls and then manually {@linkplain #refresh refreshed}.
* @param beanFactory the DefaultListableBeanFactory instance to use for this context
*/
public AnnotationConfigServletWebServerApplicationContext(DefaultListableBeanFactory beanFactory) {
super(beanFactory);
this.reader = new AnnotatedBeanDefinitionReader(this);
this.scanner = new ClassPathBeanDefinitionScanner(this);
} /**
* Create a new {@link AnnotationConfigServletWebServerApplicationContext}, deriving
* bean definitions from the given annotated classes and automatically refreshing the
* context.
* @param annotatedClasses one or more annotated classes, e.g. {@code @Configuration}
* classes
*/
public AnnotationConfigServletWebServerApplicationContext(Class<?>... annotatedClasses) {
this();
register(annotatedClasses);
refresh();
} /**
* Create a new {@link AnnotationConfigServletWebServerApplicationContext}, scanning
* for bean definitions in the given packages and automatically refreshing the
* context.
* @param basePackages the packages to check for annotated classes
*/
public AnnotationConfigServletWebServerApplicationContext(String... basePackages) {
this();
scan(basePackages);
refresh();
}
...
}
没有所谓的典型的单例代码。
所以,结论只有一个:所谓的单例是因为springboot初始化的时候只创建了一个,并不是AnnotationConfigServletWebServerApplicationContext本身是单例的。
例如有的人写代码的时候,会自动从xml中创建一个ApplicationContext。
由于单例比较简单,此处不介绍如何写了。
稍微补充下,一般情况下BeanFactory中的bean倒是单例的,这是因为按照一般的设计原则,我们不需要在bean中共享实例变量,更多是共享实例的方法而已,所以创建一个实例就够了。
这样的好处是显而易见-相对比较节约内存,因为在堆中只需要保存一份实例即可,而不是几个线程几个实例,尤其是并发高的时候,效果是非常明显,也会大大降低gc的负载。
spring与设计模式之二单例模式的更多相关文章
- java设计模式(二)单例模式,一生只爱一人,只争一朝一夕
单例模式:保证一个类在内存中的对象唯一,有且仅能实例化一次.(如多个代码块需要读取配置文件,or开启事务,orjdbc读取数据源就是个经典例子)参考:吟啸且徐行 实现步骤: 私有构造方法.保证唯一的 ...
- 10.Java设计模式 工厂模式,单例模式
Java 之工厂方法和抽象工厂模式 1. 概念 工厂方法:一抽象产品类派生出多个具体产品类:一抽象工厂类派生出多个具体工厂类:每个具体工厂类只能创建一个具体产品类的实例. 即定义一个创建对象的接口(即 ...
- Java设计模式之【单例模式】
Java设计模式之[单例模式] 何为单例 在应用的生存周期中,一个类的实例有且仅有一个 当在一些业务中需要规定某个类的实例有且仅有一个时,就可以用单例模式 比如spring容器默认初始化的实例就是单例 ...
- 【白话设计模式四】单例模式(Singleton)
转自:https://my.oschina.net/xianggao/blog/616385 0 系列目录 白话设计模式 工厂模式 单例模式 [白话设计模式一]简单工厂模式(Simple Factor ...
- 小菜学习设计模式(二)—单例(Singleton)模式
前言 设计模式目录: 小菜学习设计模式(一)—模板方法(Template)模式 小菜学习设计模式(二)—单例(Singleton)模式 小菜学习设计模式(三)—工厂方法(Factory Method) ...
- Java设计模式(二) 工厂方法模式
本文介绍了工厂方法模式的概念,优缺点,实现方式,UML类图,并介绍了工厂方法(未)遵循的OOP原则 原创文章.同步自作者个人博客 http://www.jasongj.com/design_patte ...
- Java设计模式之《单例模式》及应用场景
摘要: 原创作品,可以转载,但是请标注出处地址:http://www.cnblogs.com/V1haoge/p/6510196.html 所谓单例,指的就是单实例,有且仅有一个类实例,这个单例不应该 ...
- spring boot高性能实现二维码扫码登录(下)——订阅与发布机制版
前言 基于之前两篇(<spring boot高性能实现二维码扫码登录(上)——单服务器版>和<spring boot高性能实现二维码扫码登录(中)——Redis版>)的基础, ...
- C#设计模式学习笔记-单例模式随笔
最近学习 设计模式,从单例模式入手 啥是单例模式: 要实现一个单例类的话,首先,肯定是不能让用户自行生产的,那就是说明不能让用户new,所以,就必须把构造函数设置成为私有的 因为静态变量的生命周期跟整 ...
- IOS设计模式浅析之单例模式(Singleton)
说在前面 进入正式的设计模式交流之前,扯点闲话.我们在项目开发的过程中,经常会不经意的使用一些常见的设计模式,如单例模式.工厂方法模式.观察者模式等,以前做.NET开发的时候,认真拜读了一下程杰老师的 ...
随机推荐
- dotnet C# 反射扫描程序集所有类型会不会触发类型静态构造函数
在 dotnet 里面,有很多框架都喜欢扫描程序集进行初始化逻辑,在扫描程序集的所有类型的时候,相当于碰到所有类型.而某个类型的静态构造函数将会在某个类型被使用之前被 CLR 调用,那么扫描类型是否会 ...
- 2019-7-3-Roslyn-理解-msbuild-的清理过程
title author date CreateTime categories Roslyn 理解 msbuild 的清理过程 lindexi 2019-07-03 18:21:25 +0800 20 ...
- PostMan测试图片上传接口的方法
一.选择POST后添加接口地址 二.选择Body下的from-data 注:Headers不要加参数 三.填写key,再key后的下拉选择file,然后选择文件 注:key并不是图片名称,而是接口接收 ...
- 超好用的 Redis GUI 工具,你值得拥有
超好用的 Redis GUI 工具,你值得拥有 提供原生的性能,并且比使用 Electron 等 Web 技术开发的同等应用程序消耗的资源少得多. 下载地址:http://www.redisant.c ...
- VGA色块显示#VGA显示数字
VGA驱动色块显示 了解了VGA的显示原理和ADV7123控制后,再去实现色块显示就容易了. 像素坐标 跟显示色条不同,要在屏幕上不同的地方显示色块,需要用像素坐标来定位色块.其实,就是分别用行扫描的 ...
- Radash库使用说明——数组方法篇(全)
写在前面 tips:点赞 + 收藏 = 学会! 本文包含radash中数组相关的所有方法说明 + 使用示例 + 思维导图查看 这边会整理出一份数组相关方法的使用大纲(不含源码解析),方便大家查阅使用: ...
- nim 1. 安装、IDE、HelloWorld
2015年,某大神写过nim的教程,请参阅: Nim教程[一] - liulun - 博客园 (cnblogs.com) 七年过去了, nim应该更成熟了. 1.安装 下载页面:Windows ins ...
- JavaScript算法---基础排序类
<html> <script> //正序排序,把大的放到最后,arr[j]>arr[j+1] let fz=(arr)=>{ for(let len=arr.len ...
- flex布局方案
参考:https://blog.csdn.net/weixin_39717076/article/details/82586915
- 一种利用光电容积描记(PPG)信号和深度学习模型对高血压分类的新方法
具体的软硬件实现点击 http://mcu-ai.com/ MCU-AI技术网页_MCU-AI 据世界心脏联合会统计,截至 2022 年,全球有 13 亿人被诊断患有高血压,每年约有 1000 万人死 ...