java中属性外部化
在理解Externalizable接口之前,您需要了解序列化。您可以在java中的序列化上阅读有关序列化的更多信息。
Java提供了一种称为序列化的机制,以按字节的有序或字节序列的形式持久化Java对象,其中包括对象的数据以及有关对象的类型和存储在对象中的数据类型的信息。
可外部化:
顾名思义,这是对序列化进行外部化。如果要自定义序列化机制,则可以使用它。它使用自定义的书面机制来执行对象的编组和解组.Externalizable接口扩展了Serializable接口。 如果实现此接口,则需要重写以下方法。
@Overridepublic void readExternal(ObjectInput arg0) throws IOException,ClassNotFoundException {}@Overridepublic void writeExternal(ObjectOutput arg0) throws IOException {}
现在让我们看看序列化是如何发生的:
在发送方:
JVM检查类是否实现了可外部化。如果是,则使用writeExternal()方法对对象进行序列化。如果不实现可外部化但实现serializable,则使用ObjectOutputStream对对象进行序列化。
在接收方:
重建对象并使其可外部化时,不使用args构造函数创建实例,并调用readExternal。如果该对象不可外部化但可序列化,则使用ObjectInputStream重建对象。
让我们从与Java序列化中使用的示例相同的示例开始。
在src-> org.arpit.javapostsforlearning中创建Employee.java
Employee.java:
package org.arpit.javapostsforlearning;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;public class Employee implements Externalizable{ int employeeId;String employeeName;String department;String nationality;public Employee(){}public int getEmployeeId() {return employeeId;}public void setEmployeeId(int employeeId) {this.employeeId = employeeId;}public String getEmployeeName() {return employeeName;}public void setEmployeeName(String employeeName) {this.employeeName = employeeName;}public String getDepartment() {return department;}public void setDepartment(String department) {this.department = department;}public String getNationality() {return nationality;}public void setNationality(String nationality) {this.nationality = nationality;}@Overridepublic void readExternal(ObjectInput in) throws IOException,ClassNotFoundException {employeeId=in.readInt();employeeName=(String) in.readObject();}@Overridepublic void writeExternal(ObjectOutput out) throws IOException {out.writeInt(employeeId);out.writeObject(employeeName);}
}
如果要实现可外部化,则必须没有args构造函数。
在org.arpit.javapostsforlearning中创建ExternalizableMain.java
ExternalizableMain.java:
package org.arpit.javapostsforlearning;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;public class ExternalizableMain {/*** @author Arpit Mandliya*/public static void main(String[] args) {Employee emp = new Employee();emp.setEmployeeId(101);emp.setEmployeeName("Arpit");emp.setDepartment("CS");//Serializetry{FileOutputStream fileOut = new FileOutputStream("employee.ser");ObjectOutputStream outStream = new ObjectOutputStream(fileOut);outStream.writeObject(emp);outStream.close();fileOut.close();}catch(IOException i){i.printStackTrace();}//Deserializeemp = null;try{FileInputStream fileIn =new FileInputStream("employee.ser");ObjectInputStream in = new ObjectInputStream(fileIn);emp = (Employee) in.readObject();in.close();fileIn.close();}catch(IOException i){i.printStackTrace();return;}catch(ClassNotFoundException c){System.out.println("Employee class not found");c.printStackTrace();return;}System.out.println("Deserialized Employee...");System.out.println("Emp id: " + emp.getEmployeeId());System.out.println("Name: " + emp.getEmployeeName());}
}
运行它:当运行ExternalizableMain.java时,您将获得以下输出
Deserialized Employee...
Emp id: 101
Name: Arpit
如果您已经具有可序列化的功能,那么为什么需要完全可外部化!:
- 当您使用serializable可序列化任何对象时,除字段外,所有属于对象映射且可以使用实例变量访问的对象也将被序列化。例如:
- 如果您拥有Employee类,并且其超类是person,则它将序列化所有超类对象(例如person),直到达到“ Object”类为止。
- 同样,如果Employee具有地址类的实例变量,则它将也序列化整个地址对象图。
当您要序列化的是employeeId和employeeName时,您真的要这么多开销吗?
- 当您使用序列化速度很慢时,JVM使用反射。
- 进行序列化时,有关类描述的信息(包括其超类的描述和与该类关联的实例变量)也会存储在流中。同样,这也是性能问题
继承中的继承:
现在我们将了解继承如何影响外部化,因此可能会有很多情况说明超类是否可外部化,如果不是,那么您将如何处理它以及它是如何工作的,让我们举个例子来看。
我们将创建Person.java,它将是Employee的超类。
情况1:如果超类未实现Externalizable:
如果超类未实现externalizable,则需要在实现Externalizable的子类中序列化superclass的字段。
人.java
package org.arpit.javapostsforlearning;
public class Person {String name="default";String nationality;public Person(){System.out.println("Person:Constructor");}public Person(String name, String nationality) {super();this.name = name;this.nationality = nationality;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getNationality() {return nationality;}public void setNationality(String nationality) {this.nationality = nationality;}}
在org.arpit.javapostsforlearning中创建Employee.java
Employee.java:
package org.arpit.javapostsforlearning;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
;public class Employee extends Person implements Externalizable{ int employeeId;String department;public Employee(){}public Employee(int employeeId,String name,String department,String nationality){super(name,nationality);this.employeeId=employeeId;this.department=department;System.out.println("Employee:Constructor");}public int getEmployeeId() {return employeeId;}public void setEmployeeId(int employeeId) {this.employeeId = employeeId;}public String getDepartment() {return department;}public void setDepartment(String department) {this.department = department;}@Override
public void writeExternal(ObjectOutput out) throws IOException {/*since superclass does not implement externalizable, you need to serialize super class field in this class itself*///superclass fieldsout.writeObject(name);out.writeObject(nationality);// its own fieldsout.writeInt(employeeId);out.writeObject(department);
}@Override
public void readExternal(ObjectInput in) throws IOException,ClassNotFoundException {/*since superclass does not implement externalizable, you need to deserialize super class field in this class itself*///superclass fieldsname=(String) in.readObject();nationality=(String) in.readObject();// its own fieldsemployeeId=in.readInt();department=(String) in.readObject();}
}
在org.arpit.javapostsforlearning中创建ExternalizableMain.java
ExternalizableMain.java:
package org.arpit.javapostsforlearning;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;public class ExternalizableMain {/*** @author Arpit Mandliya*/public static void main(String[] args) {//SerializeEmployee emp = new Employee(101,"Arpit","CS","Indian");System.out.println("Before serializing");System.out.println("Emp id: " + emp.getEmployeeId());System.out.println("Name: " + emp.getName());System.out.println("Department: " + emp.getDepartment());System.out.println("Nationality: " + emp.getNationality());System.out.println("************");System.out.println("Serializing");try{FileOutputStream fileOut = new FileOutputStream("employee.ser");ObjectOutputStream outStream = new ObjectOutputStream(fileOut);outStream.writeObject(emp);outStream.close();fileOut.close();}catch(IOException i){i.printStackTrace();}//DeserializeSystem.out.println("************");System.out.println("Deserializing");emp = null;try{FileInputStream fileIn =new FileInputStream("employee.ser");ObjectInputStream in = new ObjectInputStream(fileIn);emp = (Employee) in.readObject();in.close();fileIn.close();}catch(IOException i){i.printStackTrace();return;}catch(ClassNotFoundException c){System.out.println("Employee class not found");c.printStackTrace();return;}System.out.println("After serializing");System.out.println("Emp id: " + emp.getEmployeeId());System.out.println("Name: " + emp.getName());System.out.println("Department: " + emp.getDepartment());System.out.println("Nationality: " + emp.getNationality());}
}
运行 :
当运行ExternalizableMain.java时,您将获得以下输出:
Employee:Constructor
Before serializing
Emp id: 101
Name: Arpit
Department: CS
Nationality: Indian
************
Serializing
************
Deserializing
Person:Constructor
After serializing
Emp id: 101
Name: Arpit
Department: CS
Nationality: Indian
情况2:如果超类实现了Externalizable:
如果超类实现了externalizable,那么它还将具有readExternal()和writeExternal()方法,因此它将在这些方法中序列化其自己的字段。
人.java
package org.arpit.javapostsforlearning;import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;public class Person implements Externalizable{String name="default";String nationality;public Person(){System.out.println("Person:Constructor");}public Person(String name, String nationality) {super();this.name = name;this.nationality = nationality;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getNationality() {return nationality;}public void setNationality(String nationality) {this.nationality = nationality;}@Overridepublic void writeExternal(ObjectOutput out) throws IOException {out.writeObject(name);out.writeObject(nationality);}@Overridepublic void readExternal(ObjectInput in) throws IOException,ClassNotFoundException {name=(String) in.readObject();nationality=(String) in.readObject();}}
在org.arpit.javapostsforlearning中创建Employee.java
Employee.java:
package org.arpit.javapostsforlearning;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
;public class Employee extends Person implements Externalizable{ int employeeId;String department;public Employee(){}public Employee(int employeeId,String name,String department,String nationality){super(name,nationality);this.employeeId=employeeId;this.department=department;System.out.println("Employee:Constructor");}public int getEmployeeId() {return employeeId;}public void setEmployeeId(int employeeId) {this.employeeId = employeeId;}public String getDepartment() {return department;}public void setDepartment(String department) {this.department = department;}@Overridepublic void writeExternal(ObjectOutput out) throws IOException {super.writeExternal(out);out.writeInt(employeeId);out.writeObject(department);}@Overridepublic void readExternal(ObjectInput in) throws IOException,ClassNotFoundException {super.readExternal(in);employeeId=in.readInt();department=(String) in.readObject();}
}
在org.arpit.javapostsforlearning中创建ExternalizableMain.java
ExternalizableMain.java:
package org.arpit.javapostsforlearning;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;public class ExternalizableMain {/*** @author Arpit Mandliya*/public static void main(String[] args) {//SerializeEmployee emp = new Employee(101,"Arpit","CS","Indian");System.out.println("Before serializing");System.out.println("Emp id: " + emp.getEmployeeId());System.out.println("Name: " + emp.getName());System.out.println("Department: " + emp.getDepartment());System.out.println("Nationality: " + emp.getNationality());System.out.println("************");System.out.println("Serializing");try{FileOutputStream fileOut = new FileOutputStream("employee.ser");ObjectOutputStream outStream = new ObjectOutputStream(fileOut);outStream.writeObject(emp);outStream.close();fileOut.close();}catch(IOException i){i.printStackTrace();}//DeserializeSystem.out.println("************");System.out.println("Deserializing");emp = null;try{FileInputStream fileIn =new FileInputStream("employee.ser");ObjectInputStream in = new ObjectInputStream(fileIn);emp = (Employee) in.readObject();in.close();fileIn.close();}catch(IOException i){i.printStackTrace();return;}catch(ClassNotFoundException c){System.out.println("Employee class not found");c.printStackTrace();return;}System.out.println("After serializing");System.out.println("Emp id: " + emp.getEmployeeId());System.out.println("Name: " + emp.getName());System.out.println("Department: " + emp.getDepartment());System.out.println("Nationality: " + emp.getNationality());}
}
运行 :
当运行ExternalizableMain.java时,您将获得以下输出:
Employee:Constructor
Before serializing
Emp id: 101
Name: Arpit
Department: CS
Nationality: Indian
************
Serializing
************
Deserializing
Person:Constructor
After serializing
Emp id: 101
Name: Arpit
Department: CS
Nationality: Indian
在此示例中,由于Person类在其自己的writeExternal和readExternal方法中存储和还原其字段,因此您无需在子类中保存/恢复超类字段,但是如果您仔细观察Employee类的writeExternal和readExternal方法,您将发现您仍然需要首先调用super.xxxx()方法,该方法确认可外部化对象还必须与其父类型协调才能保存和恢复其状态的语句。
可外部化的缺点:
- 如果对类定义进行了任何更改,则需要相应地维护writeExternal()和readExternal。
- 正如我们在示例中看到的,子类对象必须与其父类协调才能保存和存储其状态(通过从子类中调用super.xxxx()方法)
翻译自: https://www.javacodegeeks.com/2014/03/externalizable-in-java.html
java中属性外部化