Singleton设计模式与Spring容器中的Singleton bean相比 [英] Singleton design pattern vs Singleton beans in Spring container

查看:124
本文介绍了Singleton设计模式与Spring容器中的Singleton bean相比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

众所周知,我们在Spring容器中默认使用bean作为单例,如果我们有一个基于Spring框架的Web应用程序,那么在这种情况下我们是否真的需要实现Singleton设计模式来保存全局数据而不仅仅是创建一个通过春天的豆子。

As we all know we have beans as singleton by default in Spring container and if we have a web application based on Spring framework then in that case do we really need to implement Singleton design pattern to hold global data rather than just creating a bean through spring.

如果我无法解释我实际上要问的问题,请耐心等待。

Please bear with me if I'm not able to explain what I actually meant to ask.

推荐答案

Spring中的单例bean和单例模式完全不同。 Singleton模式表示每个类加载器将创建一个且只有一个特定类的实例。

A singleton bean in Spring and the singleton pattern are quite different. Singleton pattern says that one and only one instance of a particular class will ever be created per classloader.

Spring单例的范围被描述为每个容器每个bean 。每个Spring IoC容器的单个对象实例是bean定义的范围。 Spring中的默认范围是Singleton。

The scope of a Spring singleton is described as "per container per bean". It is the scope of bean definition to a single object instance per Spring IoC container. The default scope in Spring is Singleton.

即使默认范围是单例,也可以通过指定的范围属性来更改bean的范围< bean ../& gt; 元素。

Even though the default scope is singleton, you can change the scope of bean by specifying the scope attribute of <bean ../> element.

<bean id=".." class=".." scope="prototype" />

这篇关于Singleton设计模式与Spring容器中的Singleton bean相比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆