简体   繁体   English

等价于ObjectOutputStream,不仅保存其状态,还保存整个对象?

[英]Equivalent of ObjectOutputStream, saving not only its state but the whole object?

I'm letting the user import plugin-like classes from a remote location using URLClassLoader, so these imported classes do NOT exist in the build path (however, they all implement an interface IPlugin which is included). 我允许用户使用URLClassLoader从远程位置导入类似插件的类,因此这些导入的类在构建路径中不存在(但是,它们都实现了包含的接口IPlugin )。

I assumed one could simply use ObjectOutputStream to save all the loaded plugins to file, and later read these with ObjectInputStream. 我假设一个人可以简单地使用ObjectOutputStream将所有已加载的插件保存到文件,然后使用ObjectInputStream读取它们。 That doesn't seem to be the case though, since all it saves is the state of the object, not the containing logic (ie methods). 但是,事实似乎并非如此,因为它保存的只是对象的状态,而不是包含逻辑(即方法)。

What I'd hope to do is to save the list of loaded plugins (activePlugins) with ObjectOutputStream: 我希望做的是使用ObjectOutputStream保存已加载插件(activePlugins)的列表:

ObjectOutputStream oos = new ObjectOutputStream(*fileoutputstream*);
oos.writeObject(activePlugins);
oos.close();

Then at another runtime, load/restore all these plugins with ObjectInputStream: 然后在另一个运行时,使用ObjectInputStream加载/恢复所有这些插件:

ObjectInputStream ois = new ObjectInputStream(*fileinputstream*);
activePlugins = (ArrayList<IPlugin>) ois.readObject();

But since the actual object classes are not available in the build path (they are somewhere else on the harddrive), it goes haywire. 但是由于实际的对象类在构建路径中不可用(它们在硬盘驱动器上的其他位置),因此很麻烦。 What I'm after is some way of loading objects without having the classes available, ie loading objects with states and without dependencies. 我所追求的是在没有可用类的情况下加载对象的某种方式,即加载具有状态且没有依赖关系的对象。

you need your own classloader. 您需要自己的类加载器。 you basically want something similar to URLClassLoader, but with the ability to download and cache the jars locally. 您基本上想要类似于URLClassLoader的东西,但是能够在本地下载和缓存jar。 you might want to look at extending URLClassLoader or implementing something similar to it. 您可能想看看扩展URLClassLoader或实现类似的东西。 you basically need to just hook into the part where the jar is downloaded and stick it somewhere locally (or load it from that cached location if you already downloaded it previously). 您基本上只需要挂钩下载jar的部分并将其粘贴到本地(或者如果您先前已经下载过,则从该缓存位置加载它)。

Have a look at RMI. 看看RMI。 This extends serialization with a class transferring mechanism, so you can serialize and deserialize objects of (at the receiver) unknown classes at well, and execute their methods. 这通过类传递机制扩展了序列化,因此您可以很好地序列化(反序列化)(在接收器处)未知类的对象,并执行其方法。

This is done using some remote class loading mechanism, I think. 我认为这是使用某种远程类加载机制完成的。

Altought Java is not my primary programming framework, I have seen the same problem in other "frameworks" like PHP, Delphi and C#. Altought Java不是我的主要编程框架,我在其他“框架”(例如PHP,Delphi和C#)中也遇到了相同的问题。

One solution is to have declared class files & paths. 一种解决方案是声明类文件和路径。

The other suggestion, is that, since you expecified, you don't care about the logic, only ("data") state, you could declare a generic class that stores the value of the properties. 另一个建议是,既然您已经熟练掌握了,那么您就不必关心逻辑(仅“数据”)状态,可以声明一个通用类来存储属性的值。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM