Singleton is not really a singleton

喜欢而已 提交于 2020-01-01 12:31:14

问题


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?


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()
}

My ServletContextListener side code:

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

My singleton:

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)

回答1:


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

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.

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.




回答2:


Printing a stack trace in the constructor should give you the information you need to find out where it is being instantiated and what order. The Singleton pattern is a dangerous thing, usually better to do it a different way.




回答3:


Your ServletContextListener and main classes are loaded by different class loaders. Whenever a class gets loaded, its static block will be executed. so you will get two instances...

If you want to make sure the same classloader loads your singleton, you must specify the classloader yourself.

check this link for more details http://www.javaworld.com/article/2073352/core-java/simply-singleton.html?page=2



来源:https://stackoverflow.com/questions/18750024/singleton-is-not-really-a-singleton

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!