import java.io.ObjectStreamException; public class SingLazy { //类的初始化 不初始化这个对象(延迟加载,用到再创建) private static SingLazy instane; //私有化构造器 private SingLazy(){ //多次调用,抛出异常,防止反射 if(instane!=null){ throw new RuntimeException(); } } //方法同步,销量低 public static synchronized SingLazy gets(){ if(instane==null){ instane=new SingLazy(); } return instane; } //反序列化时,如果自定义了readResolve()则直接返回此方法指定对象,而不需要单独创建新对象 private Object readResolve() throws ObjectStreamException{ return instane; } }
防止反射和反序列化破解
最新推荐文章于 2023-12-23 14:08:37 发布