Best practice for serialization for EJB and CDI beans

后端 未结 3 828
面向向阳花
面向向阳花 2021-02-01 18:03

I have not yet experienced any serialization-related issues. But PMD and Findbugs detect a bunch of potential problems regarding seriazation. A typical case is an injected logge

3条回答
  •  不思量自难忘°
    2021-02-01 18:58

    I realize this is an old question, but I believe the only answer provided is incorrect.

    will the fields, injected by @Inject and @PersistenceContext be reinjected on deserialization?

    No, they will not. I personally experienced this with JBoss in a clustered environment. If the bean is passivation capable, then the container must inject a serializable proxy. That proxy gets serialized and deserialized. Once deserialized, it will locate the proper injection and rewire it. However, if you mark the field transient, the proxy is not serialized and you will see NPEs when the injected resource is accessed.

    It should be noted that the injected resource or bean does not have to be Serializable, because the proxy will be. The only exception is for @Dependent scoped beans which have to be serializable or the injection transient. This is because a proxy is not used in this case.

    should they be marked as transient?

    No, see above.

    or should I just ignore/switch off the code checks?

    This is up to you, but it is what I would do.

    should I really provide accessors to all those fields as PMD advises?

    No, I would not. In our projects, we disable this check when we know we are using CDI.

提交回复
热议问题