Java单例类与对象序列化
为了保证全局只有一个实例,单例类往往将构造方法的访问权限设置为private:
public class Singleton implements Serializable{
private static final Singleton SINGLETON = new Singleton(100);
private int value;
private Singleton(int value) {
// private!
this.value = value;
}
public static Singleton getInstance() {
return SINGLETON;
}
}
对单例类进行序列化需要格外小心,下面看一段代码:
public static void main(String[] args) throws IOException, ClassNotFoundException {
Singleton singleton1 = Singleton.getInstance();
ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream("singleton.dat"));
output.writeObject(singleton1);
ObjectInputStream input = new ObjectInputStream(new FileInputStream("singleton.dat"));
Singleton singleton2 = (Singleton) input.readObject();
System.out.println("创建新实例:" + (singleton1 != singleton2));
}
控制台输出:
创建新实例:true
上面代码的运行结果表明,对单例类进行序列化可能会创建新的实例,即使构造方法是私有的!
为了解决上述问题,需要定义一种被称为readResolve的特殊序列化方法。如果定义了readResolve方法,在对象序列化时就会调用它。
public class Singleton implements Serializable {
private static final Singleton SINGLETON = new Singleton(100);
private int value;
private Singleton(int value) {
// private!
this.value = value;
}
public static Singleton getInstance() {
return SINGLETON;
}
protected Object readResolve() throws ObjectStreamException {
return SINGLETON;
}
}
再次执行上面的测试代码,控制台输出如下:
创建新实例:false