简体   繁体   English

如何在每个对话中存储bean

[英]How to store a bean per conversation

Is there a way to store some bean inside the conversation context? 有没有办法在会话上下文中存储一些bean? Ie for each new conversation, a new separate bean is created belonging to it. 即,对于每个新对话,创建属于它的新的单独bean。

The easiest way to do what you want is to declare a ConversationScoped managed bean or EJB where JSF2 manages the scope. 执行所需操作的最简单方法是声明一个ConversationScoped托管bean或EJB,其中JSF2管理范围。

There are some good explanations here: 这里有一些很好的解释:

... any of which will do a better job than I will. ......任何比我更好的工作。 The very short version is that you annotate a bean - which can be a plain POJO that follows the bean conventions - with the @ConversationScoped annotation. 非常简短的版本是你使用@ConversationScoped注释来注释bean - 它可以是遵循bean约定的普通POJO。 You then @Inject a Conversation object, which you can use to begin() and end() conversations. 然后@Inject一个Conversation对象,您可以使用它来begin()end()对话。 Inject this @ConversationScoped bean into other things. 将此@ConversationScoped bean注入其他内容。 The Conversation.begin and Conversation.end methods control its lifecycle. Conversation.beginConversation.end方法控制其生命周期。

There's a bit much code to just post here, but the above links should help. 这里有一些代码可以发布,但上面的链接应该有所帮助。

An alternative to @ConversationScoped POJO managed beans can be @Stateful @ConversationScoped EJBs. @ConversationScoped POJO托管bean的替代方法可以是@Stateful @ConversationScoped EJB。 They can be really handy when you need EJB services in a conversation. 当您在对话中需要EJB服务时,它们可以非常方便。

For some of the conceptual background and detail read the CDI/Weld reference on scopes - and the rest of the CDI/Weld manual. 对于一些概念背景和细节,请阅读关于范围CDI / Weld参考 - 以及CDI / Weld手册的其余部分。 It's really well written and really good. 真的写得很好,真的很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 bean 类中存储 int 和 string 值? - How to store int and string value in bean class? 在对话作用域Bean中构建列表 - Building a List within Conversation Scoped Bean 开始CDI对话并将@ConversationScoped bean注入无状态会话bean - Starting a CDI conversation and injecting @ConversationScoped bean into stateless session bean 将 EVENT 范围的 bean 注入到 CONVERSATION/SESSION 范围的 bean 中是否更安全? - Is it safer to inject an EVENT scoped bean into a CONVERSATION/SESSION scoped bean? Univocity - 如何使用迭代器样式每行返回一个bean? - Univocity - How to return one bean per row using iterator style? 每次会话会话有什么好处? - What are the benefits of session-per-conversation? 如果使用@Bean标签创建一个bean,那么我们可以创建两个bean。 每个applicaton如何单例 - If A bean is created using @Bean tag then we can create two beans. How is it singleton per applicatonContext 如何使用LINE API和HEROKU存储LINE Bot的对话? - How can I store the conversation of LINE Bot using LINE API and HEROKU? spring singleton scope-每个bean每个容器 - spring singleton scope— per container per bean 在Spring中存储bean实例 - Store bean instance in Spring
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM