简体   繁体   English

Spring Singleton与Singleton设计模式-类加载器

[英]Spring Singleton Vs Singleton Design pattern - Class Loader

Singleton Pattern maintains the instance at Spring Container Level, while the Singleton Design Pattern maintains it at Class Loader Level. Singleton Pattern将实例维护在Spring容器级别,而Singleton Design Pattern将实例维护在Class Loader级别。

Is any other difference? 还有其他区别吗?

Next, I still think there the above reason in not a valid reason. 接下来,我仍然认为上述原因并非有效原因。 It is just the matter of fact that one Application Context/Container is loaded in one Class Loader only. 事实上,一个应用程序上下文/容器仅被加载到一个类加载器中。 Thus technically there is no difference. 因此在技术上没有区别。 Is that correct or I am missing something? 是正确的,还是我缺少了什么?

Ref: Singleton design pattern vs Singleton beans in Spring container 参考: Singleton设计模式与Spring容器中的Singleton bean

Well, the real difference is not around class loading, but it's about the design principle. 好吧,真正的区别不在于类加载,而在于设计原理。 Singleton pattern has it's own limitations. 单例模式有其自身的局限性。 It exposes an object globally as well as it's difficult to test. 它在全局范围内公开对象,并且很难测试。 But, singleton through DI framework like Spring or Guice is free from those problems. 但是,通过像Spring或Guice这样的DI框架进行单例操作不会遇到这些问题。

This SO thread may help you to understand. SO线程可以帮助您理解。 As well as Google-singleton-detector and Misko Hevery's blog are also interesting read. 以及Google-singleton-detectorMisko Hevery的博客也很有趣。

Using a "real" singleton is more limiting in the sense that you are then bound to be able to create only a single instance of that class (within a classloader). 从某种意义上说,使用“真实”单身人士的局限性更大,因为这样一来,您必然只能创建该类的单个实例(在类加载器中)。

If you use Spring singleton-scoped beans, you can create as many "singleton" instances of that class you like (as long as the bean class is not a real singleton). 如果使用Spring单例作用域的bean,则可以创建该类的任意多个“单例”实例(只要bean类不是真正的单例)。

Hence they are not technically the same thing. 因此,从技术上讲它们不是同一回事。

This is mostly the name that these two have in common. 这主要是这两个名称的共同点。 Singleton pattern ensures a class has only one instance, while Spring's singleton bean scope just instructs container to use the single instance of a bean during dependency injection, the bean can be any class with no restrictions. Singleton模式可确保一个类只有一个实例,而Spring的singleton bean范围仅指示容器在依赖项注入期间使用bean的单个实例,而bean可以是不受限制的任何类。

A spring singleton ensures that only one instance of the object is created as long as the object is created using spring framework. 弹簧单例确保只要使用spring框架创建对象,就只能创建该对象的一个​​实例。 On the contrary implementing the singleton pattern ensures that only one instance of the object is present. 相反,实现单例模式可确保仅存在该对象的一个​​实例。

I have seen code where objects were defines as singleton in spring configuration. 我已经看到了在弹簧配置中将对象定义为单例的代码。 The singleton object was some times created using spring DI and other times using new operator. 单例对象有时是使用spring DI创建的,有时是使用new运算符创建的。

Hence caution is required to ensure that such kinds of abuses are not made and single instance of singletons be maintained. 因此,需要谨慎行事,以确保不进行此类滥用,并保持单一实例的单一实例。

Singleton pattern is described at per class loader level. 在每个类加载器级别描述单例模式。 Singleton bean scope is per spring container. 单例bean范围是每个spring容器。

http://www.javabench.in/2012/04/difference-between-singleton-design.html http://www.javabench.in/2012/04/difference-between-singleton-design.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM