单身人士并不是一个单身人士

单身人士并不是一个单身人士

问题描述:

我的情况是我在运行嵌入式服务器的代码和我的Web应用程序之间共享单例。我有类和部署工具的战争。当我在printf实例中看到:

I have situation where I share singleton between my code which runs the embedded-server and my web-application. I have war with classes and deployment tool. When I printf instances I see:

abc.Abc@173a10f
abc.Abc@105738

所以这不是单身人士。这是怎么回事?

So this is not really singleton. How this works?

我的服务器Jetty开始代码:

My server Jetty start code:

public static void main(String[] args) throws Exception
{
    System.out.println(MySingleton.getInstance());
    // start Jetty here and deploy war with WebAppContext()
}

我的 ServletContextListener 边码:

public class AppServletContextListener implements ServletContextListener{
    @Override
    public void contextInitialized(ServletContextEvent arg0) {
        System.out.println(MySingleton.getInstance());
    }
}

我的单身人士:

public class MySingleton {
    private static MySingleton INSTANCE = new MySingleton();
    private MySingleton () {}
    public static MySingleton getInstance() {
        return INSTANCE;
    }
}






我在构造函数中强制异常。看起来我有两种不同。


I forced exception inside constructor. It looks like I get two different.

java.lang.Exception
        at api.MySingleton.<init>(MySingleton.java:33)
        at api.MySingleton.<clinit>(MySingleton.java:22)
        at my.project.StartJetty.main(StartJetty.java:41)
java.lang.Exception
        at api.MySingleton.<init>(MySingleton.java:33)
        at api.MySingleton.<clinit>(MySingleton.java:22)
        at api.AppServletContextListener.contextInitialized(AppServletContextListener.java:25)
        at org.eclipse.jetty.server.handler.ContextHandler.startContext(ContextHandler.java:640)
        at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:229)
        at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1208)
        at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:586)
        at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:449)
        at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:58)
        at org.eclipse.jetty.server.handler.HandlerCollection.doStart(HandlerCollection.java:224)
        at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:58)
        at org.eclipse.jetty.server.handler.HandlerWrapper.doStart(HandlerWrapper.java:89)
        at org.eclipse.jetty.server.Server.doStart(Server.java:258)
        at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:58)
        at my.project.StartJetty.main(StartJetty.java:66)


查看一些 Jetty文档。你可以使用类加载配置。

Have a look at some Jetty documentation. You can play around with class loading configurations.


如果设置为true,那么Jetty使用普通的JavaSE类加载优先级,
并给出父/系统类加载器的优先级。这避免了webapp中类的多个版本的
问题,但是父/系统加载器提供的
版本必须是以这种方式配置的所有webapp的正确版本
。 / p>

If set to true, then Jetty uses normal JavaSE classloading priority, and gives priority to the parent/system classloader. This avoids the issues of multiple versions of a class within a webapp, but the version the parent/system loader provides must be the right version for all webapps you configure in this way.

这正是您所描述的情况。一个 MySingleton 实例正由主Java程序加载,而另一个正由Jetty的类加载器加载。

This is exactly the situation you are describing. One MySingleton instance is being loaded by the main Java program and another is being loaded by Jetty's class loader.