1 Reply Latest reply on Jun 28, 2011 7:50 AM by pgmjsd

    Injecting a Session-Scoped Bean into an Application-Scoped Bean

    haye
      I'd appreciate clarifications to the following...

      When session-scoped beans are injected into application-scoped beans (or request-scoped injected into session-scoped beans):

      1. Are the session-scoped beans promoted to application scope (like with producer methods)?

      2. I understand that when you call a method on the application-scoped bean, the session-scoped beans for the active session are used during the invocation. Is this portable behavior and to be expected with all CDI implementations? If so, how do you deal with a scenario where there is no active session (e.g. in the @PostConstruct method in an @ApplicationScoped @Startup @Singleton EJB)? Or should we use "Instance<SessionScopedBean> sessionScopedBean" instead, calling sessionScopedBean.get() when a session-scoped bean is required?


      Thanks.
        • 1. Re: Injecting a Session-Scoped Bean into an Application-Scoped Bean
          pgmjsd

          Zak S wrote on Jun 27, 2011 15:53:


          I'd appreciate clarifications to the following...

          When session-scoped beans are injected into application-scoped beans (or request-scoped injected into session-scoped beans):

          1. Are the session-scoped beans promoted to application scope (like with producer methods)?


          I don't think CDI would promote the instance into the app context.  I'd guess that CDI would do the same thing that Seam 2 does: Inject the bean from the context that was active at the time.  There's nothing special about producer methods, AFAIK.  They are in dependent scope by default, so the object will be in the scope of whatever it is injected into.



          2. I understand that when you call a method on the application-scoped bean, the session-scoped beans for the active session are used during the invocation. Is this portable behavior and to be expected with all CDI implementations? If so, how do you deal with a scenario where there is no active session (e.g. in the @PostConstruct method in an @ApplicationScoped @Startup @Singleton EJB)? Or should we use Instance<SessionScopedBean> sessionScopedBean instead, calling sessionScopedBean.get() when a session-scoped bean is required?


          If there's no active session, I'd say it would inject null, or fail if the injection point requires a value.