对象序列化和持久化课件.ppt
- 【下载声明】
1. 本站全部试题类文档,若标题没写含答案,则无答案;标题注明含答案的文档,主观题也可能无答案。请谨慎下单,一旦售出,不予退换。
2. 本站全部PPT文档均不含视频和音频,PPT中出现的音频或视频标识(或文字)仅表示流程,实际无音频或视频文件。请谨慎下单,一旦售出,不予退换。
3. 本页资料《对象序列化和持久化课件.ppt》由用户(三亚风情)主动上传,其收益全归该用户。163文库仅提供信息存储空间,仅对该用户上传内容的表现方式做保护处理,对上传内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知163文库(点击联系客服),我们立即给予删除!
4. 请根据预览情况,自愿下载本文。本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
5. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007及以上版本和PDF阅读器,压缩文件请下载最新的WinRAR软件解压。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 对象 序列 持久 课件
- 资源描述:
-
1、对象序列化和持久化Object Serialization and Persistence2022-8-12Institute of Computer SoftwareNanjing University1摘要对象序列化对象持久化Language levelDatabasesHibernate2022-8-12Institute of Computer SoftwareNanjing University2摘要对象序列化对象持久化Language levelDatabasesHibernate2022-8-12Institute of Computer SoftwareNanjing Univ
2、ersity3摘要对象序列化对象持久化Language levelDatabasesHibernate2022-8-12Institute of Computer SoftwareNanjing University4Object SerializationWhyWhatHow 2022-8-12Institute of Computer SoftwareNanjing University5Java Object Serialization-WhySerialization is used for lightweight persistence and for communication v
3、ia sockets or Remote Method Invocation(RMI).2022-8-12Institute of Computer SoftwareNanjing University6Java Object Serialization-Examplepublic class Client public static void main(String args)try /Create a socket Socket soc=new Socket(InetAddress.getLocalHost(),8020);OutputStream o=soc.getOutputStrea
4、m();ObjectOutput s=new ObjectOutputStream(o);s.writeObject(Todays date);s.writeObject(new Date();s.flush();s.close();catch(Exception e)System.out.println(e.getMessage();System.out.println(Error during serialization);System.exit(1);2022-8-12Institute of Computer SoftwareNanjing University7Java Object
5、 Serialization-Examplepublic class Server public static void main(String args)ServerSocket ser=null;Socket soc=null;String str=null;Date d=null;try ser=new ServerSocket(8020);soc=ser.accept();InputStream o=soc.getInputStream();ObjectInput s=new ObjectInputStream(o);str=(String)s.readObject();d=(Date
6、)s.readObject();s.close();System.out.println(str);System.out.println(d);catch(Exception e)System.out.println(e.getMessage();System.out.println(Error during serialization);System.exit(1);2022-8-12Institute of Computer SoftwareNanjing University8Java Object Serialization-ExampleWriting to an object st
7、ream2022-8-12Institute of Computer SoftwareNanjing University9/Serialize todays date to a file.FileOutputStream f=new FileOutputStream(tmp);ObjectOutput s=new ObjectOutputStream(f);s.writeObject(Today);s.writeObject(new Date();s.flush();Java Object Serialization-ExampleReading from an object stream2
8、022-8-12Institute of Computer SoftwareNanjing University10/Deserialize a string and date from a file.FileInputStream in=new FileInputStream(tmp);ObjectInputStream s=new ObjectInputStream(in);String today=(String)s.readObject();Date date=(Date)s.readObject();Java Object Serialization-WhatObject Seria
9、lization extends the core Java Input/Output classes with support for objects.Object Serialization supports the encoding of objects,and the objects reachable from them,into a stream of bytes;and it supports the complementary reconstruction of the object graph from the stream.2022-8-12Institute of Com
10、puter SoftwareNanjing University11Java Object Serialization-GoalHave a simple yet extensible mechanism.Maintain the Java object type and safety properties in the serialized form.Be extensible to support marshaling and unmarshaling as needed for remote objects.Be extensible to support simple persiste
11、nce of Java objects.Require per class implementation only for customization.Allow the object to define its external format.2022-8-12Institute of Computer SoftwareNanjing University12Java Object Serialization-HowObjects to be saved in the stream may support either the Serializable or the Externalizab
12、le interface.For Serializable objects,the stream includes sufficient information to restore the fields in the stream to a compatible version of the class.For Externalizable objects,the class is solely responsible for the external format of its contents.2022-8-12Institute of Computer SoftwareNanjing
13、University13The Serializable Interfacepublic interface java.io.Serializable ;A Serializable class must do the following:Implement the java.io.Serializable interface Identify the fields that should be serializableHave access to the no-arg constructor of its first nonserializable superclass 2022-8-12I
14、nstitute of Computer SoftwareNanjing University14The Serializable InterfaceThe class can optionally define the following methods:writeObject(ObjectOutputStream)readObject(ObjectInputStream)writeReplace()readResolve()2022-8-12Institute of Computer SoftwareNanjing University15思考:如果一个可序列化的类实现了以上四个方法,那么
15、在序列化和反序列化的过程中,这几个方法的调用次序如何?The Externalizable Interfacepublic interface Externalizable extends Serializable public void writeExternal(ObjectOutput out)throws IOException;public void readExternal(ObjectInput in)throws IOException,java.lang.ClassNotFoundException;2022-8-12Institute of Computer Softwar
16、eNanjing University16The Externalizable InterfaceThe class of an Externalizable object must do the following:Implement the java.io.Externalizable interface Implement a writeExternal method to save the state of the objectImplement a readExternal method to read the data written by the writeExternal me
17、thod from the stream and restore the state of the object Have the writeExternal and readExternal methods be solely responsible for the format,if an externally defined format is written Have a public no-arg constructor 2022-8-12Institute of Computer SoftwareNanjing University17The Externalizable Inte
18、rfaceAn Externalizable class can optionally define the following methods:writeReplacereadResolve2022-8-12Institute of Computer SoftwareNanjing University18Note:声明类实现Externalizable接口会有重大的安全风险。writeExternal()与readExternal()方法声明为public,恶意类可以用这些方法读取和写入对象数据。如果对象包含敏感信息,则要格外小心。区别Serializable自动存储必要信息,用以反序列化
19、被存储的实例优点内建支持易于实现缺点占用空间过大速度慢Externalizable只保存被存储的类的标识,完全由程序员完成读取和写入工作优点开销较少可能的速度提升缺点虚拟机不提供帮助,程序员负担重2022-8-1219Institute of Computer SoftwareNanjing UniversityserialVersionUIDprivate static final long serialVersionUIDFor compabilityInvalidClassException It is strongly recommended that all serializable
20、 classes explicitly declare serialVersionUID valuesserialver;eclipse2022-8-12Institute of Computer SoftwareNanjing University20Serialization Principles如果该类有父类如果父类实现了可序列化接口,则OK如果父类没有实现可序列化接口,则父类所有字段的属性默认情况下不会被序列化如果该类的某个属性标识为static类型的,则该属性不能序列化;如果该类的某个属性采用transient关键字标识,则该属性不能序列化;2022-8-12Institute of
21、 Computer SoftwareNanjing University21Serialization Principles在我们标注一个类可以序列化的时候,其以下属性应该设置为transient来避免序列化:线程相关的属性;需要访问IO、本地资源、网络资源等的属性;没有实现可序列化接口的属性;2022-8-12Institute of Computer SoftwareNanjing University22Some Items from Effective Java2022-8-12Institute of Computer SoftwareNanjing University23Effe
22、ctive Java for Serialization1.Implement Serializable judiciously 谨慎地实现Serializable代价1:一旦一个类被发布,则“改变这个类的实现”的灵活性将大大降低。序列化会使类的演化受到限制。代价2:增加了错误和安全漏洞的可能性。序列化机制是一种语言之外的对象创建机制。代价3:随着一个类的新版本的发行,相关的测试负担增加了。可序列化类的变化越大,它就越需要测试。2022-8-12Institute of Computer SoftwareNanjing University24Effective Java for Serial
23、izationNotes:为了继承而设计的类应该很少实现Serializable,接口也应该很少会扩展它。对于为继承而设计的不可序列化的类,应该考虑提供一个无参数的构造函数。内部类应该很少实现Serializable。2022-8-12Institute of Computer SoftwareNanjing University25Effective Java for Serialization2.Consider using a custom serialized form 考虑使用自定义的序列化形式如果一个对象的物理表示等同于它的逻辑内容,则默认的序列化形式可能是合适的。即使确定了默认序
24、列化形式是合适的,通常仍然要提供一个readObject方法以保证约束关系和安全性。2022-8-12Institute of Computer SoftwareNanjing University26Effective Java for Serialization2022-8-12Institute of Computer SoftwareNanjing University27Effective Java for Serialization2022-8-12Institute of Computer SoftwareNanjing University28Effective Java fo
25、r Serialization当一个对象的物理表示与它的逻辑数据内容有实质性的区别时,使用默认序列化形式有4个缺点:它使这个类的导出API永久地束缚在该类的内部表示上。它要消耗过多的空间。它要消耗过多的时间。它会引起栈溢出。2022-8-12Institute of Computer SoftwareNanjing University292022-8-12Institute of Computer SoftwareNanjing University30Effective Java for Serialization2022-8-12Institute of Computer Softwar
展开阅读全文