问题
How is the session defined in @SessionScoped
CDI bean?
Is this annotation valid only when called from Servlet container, where the session is well defined in form of HttpSession
?
If not, than how an EJB with @Inject @SessionScoped MyBean myBean
can know what the session really is? I mean, methods of this EJB could have been invoked by a standalone client, RESTful WS or by some other view.
What should happen in such case? Should the annotation have no meaning, should it inject fresh MyBean
instance for each request or maybe it should retain the same instance across all requests?
回答1:
Taken from the @SessionScoped specification
The session scope is active:
during the service() method of any servlet in the web application, during the doFilter() method of any servlet filter and when the container calls any HttpSessionListener, AsyncListener or ServletRequestListener.
So in short, yes. It is bound to the HttpSession. Also:
The session context is shared between all servlet requests that occur in the same HTTP session. The session context is destroyed when the HTTPSession times out, after all HttpSessionListeners have been called, and at the very end of any request in which invalidate() was called, after all filters and ServletRequestListeners have been called.
来源:https://stackoverflow.com/questions/8234406/how-does-sessionscoped-work-with-ejb-is-cdi-only-for-web-tier