国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

java - 為什么Serializable中定義的Class 不能序列化?

瀏覽:157日期:2023-10-20 09:14:14

問題描述

Fields in a Serializable class must themselves be either Serializable or transient even if the class is never explicitly serialized or deserialized. That’s because under load, most J2EE application frameworks flush objects to disk, and an allegedly Serializable object with non-transient, non-serializable data members could cause program crashes, and open the door to attackers.This rule raises an issue on non-Serializable fields, and on collection fields when they are not private (because they could be assigned non-Serializable values externally), and when they are assigned non-Serializable types within the class.Noncompliant Code Example

public class Address { //...}public class Person implements Serializable { private static final long serialVersionUID = 1905122041950251207L; private String name; private Address address; // Noncompliant; Address isn’t serializable}

問題解答

回答1:

一個對象序列化時,按照Java默認的序列化規則,對象內的所有成員都要序列化,也就是說,這些Class都必須實現Serializable。

所以,你有兩種改法,一是Address實現Serializable接口,二是對Person中的address成員加上transient標記,這樣該成員就不會被序列化進去。

回答2:

如果 address 成員需要進行序列化的話,則Address類也需要實現Serializable接口。如果 address 成員不需要進行序列化的話,可以加上transient關鍵字,則address成員不做序列化操作,值為null。如下:

public class Person implements Serializable { private static final long serialVersionUID = 1905122041950251207L; private String name; private transient Address address; // Noncompliant; Address isn’t serializable}

當然還有其他方式:比如實現Externalizable接口,重寫readExternal(ObjectInput in)和writeExternal(ObjectOutput out)方法。還有一個替代實現Externalizable接口方法,還是實現Serializable接口,添加writeObject(ObjectOutputStream obs)和readObject(ObjectInputStream ois)方法。

再說說為什么Address一定要實現Serializable,或者加上transient關鍵字Person才能進行序列化?先看看不做處理,使用 ObjectOutputStream 來持久化對象,拋出的異常

Exception in thread 'main' java.io.NotSerializableException

ObjectOutputStream源碼:

/** * Underlying writeObject/writeUnshared implementation. */ private void writeObject0(Object obj, boolean unshared)throws IOException { //...... // remaining cases if (obj instanceof String) {writeString((String) obj, unshared); } else if (cl.isArray()) {writeArray(obj, desc, unshared); } else if (obj instanceof Enum) {writeEnum((Enum<?>) obj, desc, unshared); } else if (obj instanceof Serializable) {writeOrdinaryObject(obj, desc, unshared); } else {if (extendedDebugInfo) { throw new NotSerializableException(cl.getName() + 'n' + debugInfoStack.toString());} else { throw new NotSerializableException(cl.getName());} }} finally { depth--; bout.setBlockDataMode(oldMode);} }

從此可知, 如果被寫對象類型是String、Array、Enum、Serializable,就可以進行序列化,否則將拋出NotSerializableException。且在序列化對象時,不僅會序列化當前對象本身,還會對該對象引用的其它對象也進行序列化。

標簽: java
相關文章:
主站蜘蛛池模板: 亚洲精品免费视频 | 99久在线观看| 欧美人禽杂交狂配毛片 | 97视频在线免费 | 成人免费网站在线观看 | 亚洲精品成人一区二区aⅴ 亚洲精品成人一区二区www | 国产aⅴ一区二区 | 国产香蕉成人综合精品视频 | 伊人久久大香线焦综合四虎 | 毛片图片 | 亚洲视频一区在线观看 | 亚洲午夜精品在线 | 91成人精品视频 | 欧美日韩亚洲国产 | 特级做人爱c级特级aav毛片 | 一区在线观看 | 波少野结衣在线播放 | 亚洲国产精品一区二区三区 | 国产一区二区三区日韩欧美 | 超矿碰人人超人人看 | 99久久精品国产一区二区成人 | chinese耄耋70老太性 | 香港一级纯黄大片 | 亚洲高清二区 | 欧洲亚洲综合一区二区三区 | 美女视频永久黄网站在线观看 | 欧美亚洲日本韩国一级毛片 | 午夜视频在线观看一区二区 | 久草高清在线 | 成人影院一区二区三区 | 曰韩一级| 日本黄页网站免费大全 | 欧美综合亚洲 | 亚洲成年人免费网站 | 成人av手机在线观看 | 欧美日韩成人午夜免费 | 深夜福利视频在线看免费 | 日韩在线播放中文字幕 | 456主播喷水在线观看 | 一级黄色香蕉视频 | 天天看片天天爽 |