1 Reply Latest reply on Sep 17, 2010 8:35 AM by Marcel Kolsteren

    ApplicationScoped and SingletonEJB

    nimo stephan Master

      I want to use a stateless singleton EJB:


      Can I use this in Weld:


      @Singleton
      @Startup
      public class MySingletonEJB {
      ..
      }




      Or should I prefer this:



      @ApplicationScoped
      public class MySingletonWebBean {
      
      @PostConstruct
      public void init(){
      ..
      }
      ..
      }



      I am unsure because of the statement of Weld-Documentation:
      5.4. The singleton pseudo-scope



      A fourth, better solution is to instead use @ApplicationScoped, allowing the container to proxy the bean, and take care of serialization problems automatically.

      However, I have thought that EJB-Container ensures to solve these problems automatically when using EJBs. Am I right?

        • 1. Re: ApplicationScoped and SingletonEJB
          Marcel Kolsteren Apprentice

          The problem is that you are comparing EJB Singletons with CDI application-scoped beans, while section 5.4 of the Weld documentation compares CDI singletons to CDI application-scoped beans.


          So, in fact there are three ways of implementing a singleton:
          - Use the EJB 3.1 singleton (annotation @javax.ejb.Singleton)
          - Use a CDI bean in singleton pseudo scope (annotation @javax.inject.Singleton)
          - Use a CDI bean in application scope


          The fact that JEE6 has two Singleton concepts with the same @Singleton annotation is quite confusing. I hope that CDI and EJB will blend further in JEE7. :-)