在实际项目开发中,肯定在实体类中见到过serialVersionUID所声明的常量,有没有想过这个serialVersionUID有什么作用呢?
类实现Serializable接口的目的是为了对象可持久化,比如网络传输或本地存储,为系统的分布和异构部署提供先决条件支持。若没有序列化,现在熟悉的远程调用、对象数据库都不可能存在;
import java.io.Serializable;
public class Person implements Serializable {
private String name;
private Integer age;
//setter/getter省略...
}
public class Producer {
public static void main(String[] args) {
Person p = new Person();
p.setName("张三");
p.setAge(21);
// 序列化,保存到磁盘上
SerializationUtils.writeObject(p);
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class SerializationUtils {
private static String FILE_NAME = "d:/obj.bin";
//序列化
public static void writeObject(Serializable s) {
try {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(FILE_NAME));
oos.writeObject(s);
oos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//反序列化
public static Object readObject() {
Object obj = null;
try {
ObjectInputStream input = new ObjectInputStream(new FileInputStream(FILE_NAME));
obj=input.readObject();
input.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return obj;
}
}
public class Customer {
public static void main(String[] args) {
//反序列化
Person p=(Person) SerializationUtils.readObject();
System.out.println(p.getName());
}
}
public class Person implements Serializable {
private String name;
private Integer age;
private String sex;
//setter/getter省略...
}
public class Producer {
public static void main(String[] args) {
Person p = new Person();
p.setName("张三");
p.setAge(21);
p.setSex("男");
// 序列化,保存到磁盘上
SerializationUtils.writeObject(p);
}
}
public class Person implements Serializable {
private String name;
private Integer age;
//setter/getter省略...
}
public class Customer {
public static void main(String[] args) {
//反序列化
Person p = (Person) SerializationUtils.readObject();
System.out.println(p.getName() + " , " + p.getAge());
}
}
private static final long serialVersionUID = 7111078431122519129L;
修改一下示例代码,在生产者和消费者的Person类中同时添加private static final long serialVersionUID = 7111078431122519129L;
public class Person implements Serializable {
//添加serialVersionUID
private static final long serialVersionUID = 7111078431122519129L;
private String name;
private Integer age;
private String sex;
//setter/getter省略...
}
public class Producer {
public static void main(String[] args) {
Person p = new Person();
p.setName("张三");
p.setAge(21);
p.setSex("男");
// 序列化,保存到磁盘上
SerializationUtils.writeObject(p);
}
}
public class Person implements Serializable {
//添加serialVersionUID
private static final long serialVersionUID = 7111078431122519129L;
private String name;
private Integer age;
//setter/getter省略...
}
public class Customer {
public static void main(String[] args) {
//反序列化
Person p = (Person) SerializationUtils.readObject();
System.out.println(p.getName() + " , " + p.getAge());
}
}
一起来看看,当生产者成员变量(无age变量、sex变量)<消费者成员变量(有age变量、sex变量)时,会出现什么情况:
public class Person implements Serializable {
private static final long serialVersionUID = 7111078431122519129L;
private String name;
//setter/getter省略...
}
public class Producer {
public static void main(String[] args) {
Person p = new Person();
p.setName("张三");
// 序列化,保存到磁盘上
SerializationUtils.writeObject(p);
}
}
public class Person implements Serializable {
private static final long serialVersionUID = 7111078431122519129L;
private String name;
private Integer age;
private String sex;
//setter/getter省略...
public class Customer {
public static void main(String[] args) {
//反序列化
Person p = (Person) SerializationUtils.readObject();
System.out.println(p.getName() + " , " + p.getAge()+ " , " +p.getSex());
}
}