-1

我对单例模式有一些概念性的想法,我对单例如何在不同的类加载器中工作感到困惑。我试图找出这一点,但无法得到确切的想法。单例对象基本上是每个类加载器的单个实例。对于分布式应用程序,我的服务器上部署了多个应用程序,并且我在一个应用程序中创建了一个单例对象,当我尝试从另一个应用程序访问这个单例对象时,因为类加载器不同,它将创建一个新实例。怎么控制??

4

2 回答 2

0

In the case of distributed applications, where i have more than one application deployed on my server and i have created a singleton object in one application , when i am trying to access this singleton object from another application since the classloader is different it will create a new instance

Multiple classloaders on multiple JVMs will cause singleton pattern to break as you will have several instances of that class.

Probably the best use for a singleton in an clustered app server is when the singleton is totally state-less(These are created while the ApplicationContext is being initialized. The same instance will be returned/injected during the lifetime of this ApplicationContext.), and is only used as a convenience to access global data/functions.

于 2013-06-30T06:56:37.083 回答
0

服务器上的不同应用程序不应相互干扰。这就是服务器为它们创建不同的类加载器的原因。它们的行为就好像它们在不同的 JVM 上运行一样。

于 2013-06-30T07:19:30.227 回答