如何在Web应用程序中处理单例? [英] How are singletons handled in a web application?

查看:116
本文介绍了如何在Web应用程序中处理单例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我的理解,单例基本上就是当你有一个私有成员代表你想拥有单个实例的对象时。然后在构造函数中初始化成员对象。

From what I understand, a singleton is basically when you have a private member that represents the object you want to have a single instance for. Then in the constructor you initialize the member object.

此对象的所有引用都是通过公共属性完成的,而public属性只引用已经存在的私有成员实例化。

All references for this object are done via a public property, and the public property just references the private member that has already been instantiated.

现在在Web应用程序中,这是如何工作的?在tomcat关闭之前,单个实例是否只在容器中挂起(比如tomcat)?

Now in a web application, how does this work? Does a single instance just hang around in the container (say tomcat) until tomcat is shutdown?

推荐答案

如果您的执行环境使用多个类加载器,然后你得到每个类的实例一个单例。如果您的单例类被加载到不同的类加载器中,那么它实际上是两个不同的类,然后将有两个单例实例。

If your execution environment uses multiple class loaders, then you get one singleton per instance of your class. If your singleton class is loaded into different class loaders, then it's actually two distinct classes and then there will be two "singleton" instances.

您可以在文档中 Tomcat类加载器

这篇关于如何在Web应用程序中处理单例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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