- Java EE 6
- CDI
- JSF 2
- EJB 3.1
- Spring Security
我为我的JSF页面提供了基于CDI的后备bean(@ViewScoped,@Named)。
我使用@Stateless EJB bean来完成实际工作。
我只需要很少的会话信息,如jSessionCookie(由容器管理),内部用户名和一些其他内部标识。现在,我不知道在哪里放置这个会话信息,以便我可以在JSF的后备bean中访问它,还可以将它提供给无状态的EJB?我应该使用@Stateful EJB会话bean,还是应该使用@SessionScoped和@Named来创建基于CDI的POJO?
有没有最佳实践?
解决方案对于您的特定用例,有状态会话bean不会是一个不错的选择。请注意,与人们可能声称的有争议的是,有状态的会话bean肯定不是您通常应该避免的事情。然而,它们是针对高级用例的,例如在处理JPA的扩展持久性上下文时。
有状态会话bean在这里不工作的原因是它们不是自动与HTTP会话相关联,这似乎是您的首要关注点。您可以向其添加@SessionScoped注释,但是您也可以使用常规托管bean。您不会使用SFSB的任何特殊功能。
请参阅alo:
- 可以我使用具有CDI的EJB无状态Bean来维护用户会话?
- 使用有状态会话Bean跟踪用户的会话
- 调用CDI会话作用域生成器方法一个EJB无状态会话bean
- Java EE 6中的上下文和依赖注入
你可以注入你的statele具有会话范围CDI bean的ss EJB,但是您必须意识到,在同一应用程序中,您的EJB bean将依赖于HTTP会话(有时您希望避免的情况,例如,如果您的bean也必须从其他上下文调用)。
I'm working on a Java webapp trying to combine the following technologies:
- Java EE 6
- CDI
- JSF 2
- EJB 3.1
- Spring Security
I provide CDI-based backing beans (@ViewScoped, @Named) for my JSF pages.
I use @Stateless EJB beans for the actual work to be done.
I only need few session information like jSessionCookie (managed by container), internal username and some other internal IDs. Now, I wonder where to put this session information so that I can access it in the backing beans for JSF, but also provide it to the stateless EJBs? Should I use a @Stateful EJB session bean or should I create CDI-based POJO with @SessionScoped and @Named?
Are there any best practices?
解决方案For your particular use case, a stateful session bean would not be a good choice.
Do note that contrary to what people may claim, stateful session beans are surely not something you should generally avoid. However, they are for advanced use cases, for instance when dealing with JPA's extended persistence context.
The reason why stateful session beans would not work here, is that they are not automatically associated with the HTTP session, which seems to be your prime concern. You could add the @SessionScoped annotation to them, but then you could just as well use a regular managed bean. You would not use any of the particular features of a SFSB.
See alo:
- Can I use EJB Stateless Bean with CDI to maintain user session?
- Using a Stateful Session Bean to track an user's session
- Calling a CDI session scoped producer method from an EJB stateless session bean
- Contexts and Dependency Injection in Java EE 6
You can inject your stateless EJBs with a session scoped CDI bean, but you have to realize that within the same application your EJB bean would be dependent on the HTTP session then (something you sometimes want to avoid, e.g. if your bean has to be called from other contexts as well).
更多推荐
在有状态的EJB 3.1 bean中保持会话?
发布评论