【正文】
對象序列化和持久化 Object Serialization and Persistence 2020/11/4 Institute of Computer Software Nanjing University 1 摘要 ? 對象序列化 ? 對象持久化 ?Language level ?Databases ? Hibernate 2020/11/4 Institute of Computer Software Nanjing University 2 摘要 ? 對象序列化 ? 對象持久化 ?Language level ?Databases ? Hibernate 2020/11/4 Institute of Computer Software Nanjing University 3 摘要 ? 對象序列化 ? 對象持久化 ?Language level ?Databases ? Hibernate 2020/11/4 Institute of Computer Software Nanjing University 4 Object Serialization ? Why ? What ? How 2020/11/4 Institute of Computer Software Nanjing University 5 Java Object Serialization Why ? Serialization is used for lightweight persistence and for munication via sockets or Remote Method Invocation (RMI). 2020/11/4 Institute of Computer Software Nanjing University 6 Java Object Serialization Example public class Client { public static void main(String args[]) { try { // Create a socket Socket soc = new Socket((), 8020)。 OutputStream o = ()。 ObjectOutput s = new ObjectOutputStream(o)。 (Today39。s date)。 (new Date())。 ()。 ()。 } catch (Exception e) { (())。 (Error during serialization)。 (1)。 } } } 2020/11/4 Institute of Computer Software Nanjing University 7 Java Object Serialization Example public 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 = ()。 InputStream o = ()。 ObjectInput s = new ObjectInputStream(o)。 str = (String) ()。 d = (Date) ()。 ()。 (str)。 (d)。 } catch (Exception e) { (())。 (Error during serialization)。 (1)。 } } } 2020/11/4 Institute of Computer Software Nanjing University 8 Java Object Serialization Example ? Writing to an object stream 2020/11/4 Institute of Computer Software Nanjing University 9 // Serialize today39。s date to a file. FileOutputStream f = new FileOutputStream(tmp)。 ObjectOutput s = new ObjectOutputStream(f)。 (Today)。 (new Date())。 ()。 Java Object Serialization Example ? Reading from an object stream 2020/11/4 Institute of Computer Software Nanjing University 10 // Deserialize a string and date from a file. FileInputStream in = new FileInputStream(tmp)。 ObjectInputStream s = new ObjectInputStream(in)。 String today = (String)()。 Date date = (Date)()。 Java Object Serialization What ? Object Serialization 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 plementary reconstruction of the object graph from the stream. 2020/11/4 Institute of Computer Software Nanjing University 11 Java Object Serialization Goal ? Have 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 persistence of Java objects. ? Require per class implementation only for customization. ? Allow the object to define its external format. 2020/11/4 Institute of Computer Software Nanjing University 12 Java Object Serialization How ? Objects to be saved in the stream may support either the Serializable or the Externalizable interface. ?For Serializable objects, the stream includes sufficient information to restore the fields in the stream to a patible version of the class. ?For Externalizable objects, the class is solely responsible for the external format of its contents. 2020/11/4 Institute of Computer Software Nanjing University 13 The Serializable Interface ? public interface { }。 ? A Serializable class must do the following: ?Implement the interface ?Identify the fields that should be serializable ?Have access to the noarg constructor of its first nonserializable superclass 2020/11/4 Institute of Computer Software Nanjing University 14 The Serializable Interface ? The class can optionally define the following methods: ?writeObject (ObjectOutputStream) ?readObject (ObjectInputStream) ?writeReplace () ?readResolve () 2020/11/4 Institute of Computer Software Nanjing University 15 思考:如果一個可序列化的類實現(xiàn)了以上四個方法,那么在序列化和反序列化的過程中,這幾個方法的調(diào)用次序如何? The Externalizable Interface public interface Externalizable extends Serializable { public void writeExternal(ObjectOutput out) throws IOException。 public void readExternal(ObjectInput in) throws IOException, 。 } 2020/11/4 Institute of Computer Software Nanjing University 16 The Externalizable Interface ? The class of an Externalizable object must do the following: ? Implement the interface ? Implement a writeExternal method to save the state of the object ? Implement a readExternal method to read the data written by the writeExternal method 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 noarg constructor 2020/11/4 Institute of Computer Software Nanjing University 17 The Externalizable Interface ? An Externalizable class can optionally