@SessionScoped如何与EJB一起使用? CDI是否仅适用于Web层? [英] How does @SessionScoped work with EJB? Is CDI only for web-tier?

查看:58
本文介绍了@SessionScoped如何与EJB一起使用? CDI是否仅适用于Web层?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

会话如何在 @SessionScoped CDI bean中定义?

此注释仅在从Servlet调用时有效吗?容器,其中以 HttpSession 的形式很好地定义了会话?

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?

如果不是,那么使用 @Inject @SessionScoped MyBean myBean 可以知道会话实际上是什么吗?我的意思是,该EJB的方法可能已由独立的客户端,RESTful WS或其他视图调用。

在这种情况下会发生什么?注释应该没有任何意义,是否应该为每个请求注入新的 MyBean 实例,或者应该在所有请求中保留相同的实例?

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?

推荐答案

来自 @ SessionScoped规范


会话范围处于活动状态:

The session scope is active:

在Web应用程序中任何servlet的service()方法期间,
在任何servlet过滤器的doFilter()方法期间,以及
容器调用任何HttpSessionListener,AsyncListener或
ServletRequestListener。

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.

总之,是的。它绑定到HttpSession。另外:

So in short, yes. It is bound to the HttpSession. Also:


会话上下文在同一HTTP会话中发生
的所有servlet请求之间共享。在
HTTPSession超时后,所有HttpSessionListeners被调用
之后,以及在调用了invalidate()
的任何请求的最后,在所有过滤器和ServletRequestListeners已被调用

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.

这篇关于@SessionScoped如何与EJB一起使用? CDI是否仅适用于Web层?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆