将具有瞬态属性的对象写入流 (Java)

发布于 2024-09-07 07:14:13 字数 289 浏览 4 评论 0原文

我想将一个对象及其瞬态属性写入流(或字节数组)中,以便能够在另一个虚拟机中重建它。我不想修改它的属性,因为该对象是遗留应用程序的一部分。

标准 Java 序列化机制没有帮助。我还有什么其他选择?

更新: 我问这个问题的原因是我想修改现有的 Spring 应用程序。它之前在进程中调用了 bean 的方法,但现在我想将 bean 移动到单独的机器上并通过 HTTP 调用程序使用 Spring 远程处理。我对具有瞬态字段的参数有问题,这些字段需要传递给此方法,但不需要在应用程序的其他部分中序列化。

I want to write an object into a stream (or byte array) with its transient attributes to be able to reconstruct it in another VM. I don't want to modify its attributes because that object is a part of legacy application.

Standard Java serialization mechanism doesn't help. What other options do I have?

Update:
The reason I'm asking the question is that I want to modify an existing Spring application. It called a bean's method in-process earlier but now I want to move the bean on a separate machine and use Spring remoting through HTTP invoker. And I have a problem with parameters that have transient fields that need to be passed to this method but not needed to be serialized in other parts of the app.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

长不大的小祸害 2024-09-14 07:14:13

嗯 - 如果一个属性被标记为transient,则意味着完全它不应该被视为对象持久状态的一部分,例如序列化。事实上,您想要这样做是一种代码味道,正确的解决方案是阻止这些字段处于瞬态。

假设无论出于何种原因您都无法修改目标类本身。我的第一个想法是,您可以通过实现 readObject() 和 writeObject() 方法来自定义序列化,但这也需要对目标类进行更改。

在这种情况下,您需要使用某种基于反射或基于元数据的 API 才能执行此操作。有许多库可以将对象与 XML 或 JSON 或 DB 行等相互转换。您最好的选择是使用其中之一将对象与“水合”形式相互转换(并且可能需要自定义它们,因为任何理智的序列化程序都会忽略瞬态字段)。选择哪一种取决于您当前的软件堆栈以及您的具体要求。

Hmm - if an attribute is marked as transient, that means exactly that it's not mean to be considered part of the object's persistent state, e.g. for serialization. The fact that you want to do this at all is a code smell, and the correct solution is to stop those fields being transient.

Let's say though that for whatever reason you can't modify the target classes themselves. My first thought was that you could customise the serialisation by implementing readObject() and writeObject() methods, but that would also require changes to the target class.

In that case, you'll need to work with some kind of reflection-based or metadata-based API in order to do this. There are many libraries that will convert objects to and from XML or JSON or DB rows, etc. Your best bet would be to use one of these to convert the object to and from "hydrated" form (and likely you'll need to customise them, as any sane serialiser will ignore transient fields). Which one to pick depends on your current software stack, and your precise requirements.

℉絮湮 2024-09-14 07:14:13

我假设您无法更改遗留代码。在这种情况下,我认为您将不得不使用 反射DataOutputStream

I assume you cannot change the legacy code. In this case I think you will have to resort to going over the object fields with reflection and DataOutputStream.

因为看清所以看轻 2024-09-14 07:14:13

瞬态变量应该是那些不可序列化或易于重新计算的变量。

我的第一个建议是寻找该对象上的方法来重新计算瞬态字段。

transient variables are supposed to be those that aren't serializable or are easily recalculated.

My first suggestion is to look for methods on this object to recalculate the transient fields.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文