How does @SessionScoped work with EJB? Is CDI only for web-tier?

拜拜、爱过 提交于 2019-12-22 04:07:37

问题


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

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