@ApplicationScoped必须可序列化吗?

编程入门 行业动态 更新时间:2024-10-10 13:19:03
本文介绍了@ApplicationScoped必须可序列化吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我现在正在 Java EE 7中了解CDI范围教程,并在最后一段中找到

I am now learning about CDI scope in Java EE 7 tutorial and finding that in the last paragraph it says

使用会话,应用程序或会话范围的Bean必须可序列化,但是使用请求范围的bean不必是可序列化的。

Beans that use session, application, or conversation scope must be serializable, but beans that use request scope do not have to be serializable.

但是令我感到困惑的是在我的IDE中(Netbeans和IntelliJ Idea),当我使用@SessionScoped或@ConversationScoped时,如果我没有像Java EE 7教程所说的那样实现Serializable,它的确给我一个错误,显然,我无法构建该项目然后运行它。当我使用@ApplicationScoped但未实现Serializable时,事情变得很奇怪,没有错误出现,我可以构建然后正常运行该应用程序。

But what confused me a lot is that in my IDE (Netbeans and IntelliJ Idea), when I use @SessionScoped or @ConversationScoped, it does give me an error if I am not implementing the Serializable just like what the Java EE 7 tutorial has said, and obviously, I can't build the project then run it. Things get strange when I use @ApplicationScoped but not implementing the Serializable, no errors come out and I can build then run the application normally.

所以我很好奇那真的很想知道为什么。您能解释一下发生了什么吗?

So I'm very curious about that and really want to know why. Could you please explain what happened there? Thank you so much!

推荐答案

显示IDE中的错误主要是因为您的IDE对此有一些插件(不是被信任100%btw)。

The errors in your IDE are showed basically because your IDE has some plugin for this (which is not to be trusted 100% btw).

序列化的原因如下:

  • @SessionScoped bean

  • 这些由只是CDI规范和其他规范对此有要求
  • 即,容器可以选择存储钝化会话以节省资源
  • 另一个故事是服务器之间的复制,因此您的请求可以在多个节点上处理(故障转移方案等)。
  • 我们不知道为什么,但是因此,我们必须确保此类Bean是始终可序列化
  • These are handled by more than just CDI spec and other specs have requirements on them
  • Namely, container can choose to store the passivate the session on order to preserve resources
  • Another story is replication between servers so your requests can be handled on several nodes (failover scenario etc.)
  • We don't know why but because of this we must make sure such beans are always serializable

@ConversationScoped 豆

  • 几乎是相同的故事,并不是其他规范对此有要求,但这些bean仍然存在在会话范围内,并且可以生存到会话中(如果不尽快结束)
  • 由于这些原因,当服务器与会话范围内的bean一起钝化/复制会话时,它当然也必须钝化/复制对话范围的Bean

@ApplicationScoped bean

  • 这些确实应该可序列化,但在某些情况下,即使它们不是
  • 在这种情况下,您可以避免序列化,例如在单节点应用程序服务器上运行
  • 一旦您需要将此类bean复制到多个节点中,就需要它也可序列化
  • 另一个在SE中可以忽略可序列化性的情况
  • 因此,您的IDE很聪明,不会强制 Serializable 存在
  • These really should be serializable but under certain circumstances, your app will work even when they are not
  • These circumstances being when you can avoid serialization, e.g. running on a single node application server
  • As soon as you need to replicate such bean into several nodes, you need it to be serializable too
  • Another case when serializability can be omitted is in SE
  • Your IDE is therefore smart and does not mandate the Serializable presence

更多推荐

@ApplicationScoped必须可序列化吗?

本文发布于:2023-11-25 09:19:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1629193.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:序列化   ApplicationScoped

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!