Announcement Announcement Module
No announcement yet.
Singleton and SPRING Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Singleton and SPRING


    I have a POJO java class which I would like it to be singleton throughout the whole application server (cross all modules). Is it possible?

    Specifically, I have serveral modules in an application suite. On my application server, there are serveral jar and war files, each corresponds to one module. Together, they form an application suite. Each module has its own "applicationcontext.xml" for SPRING configuration. My global singleton object should be shared by all these modules (the instance of the object accessed in one module should be the same as the one in another module). It seems impossible, right? I know each independent module with its "applicationcontext.xml" is loaded by different class loader. SPRING has no problem to support singleton in one module, but "cross-module singleton" seems impossible to be supported by SPRING (or by any means)?

    Thanks for any input!

  • #2
    There is the ContextSingletonBeanFactoryLocator class. See:

    Of course, with servers, there are many pitfalls regarding classloaders that you have to be mindful of.


    • #3
      Yes, ContextSingletonBeanFactoryLocator is not immune to classloader issues. I would consider binding your singleton to JNDI, or some other context that is truly guaranteed to be unique acroos the application server.


      • #4
        Or you can roll up the modules into an EAR file and expose your singleton from the EAR classloader.

        Or, if you don't want to bother, just put it on the server classpath - which is (very) bad practice if you ask me, but, hey, we all cut corners from time to time.