色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術(shù)文章
文章詳情頁

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

瀏覽:113日期: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:

一個對象序列化時,按照J(rèn)ava默認(rèn)的序列化規(guī)則,對象內(nèi)的所有成員都要序列化,也就是說,這些Class都必須實(shí)現(xiàn)Serializable。

所以,你有兩種改法,一是Address實(shí)現(xiàn)Serializable接口,二是對Person中的address成員加上transient標(biāo)記,這樣該成員就不會被序列化進(jìn)去。

回答2:

如果 address 成員需要進(jìn)行序列化的話,則Address類也需要實(shí)現(xiàn)Serializable接口。如果 address 成員不需要進(jìn)行序列化的話,可以加上transient關(guān)鍵字,則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}

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

再說說為什么Address一定要實(shí)現(xiàn)Serializable,或者加上transient關(guān)鍵字Person才能進(jìn)行序列化?先看看不做處理,使用 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,就可以進(jìn)行序列化,否則將拋出NotSerializableException。且在序列化對象時,不僅會序列化當(dāng)前對象本身,還會對該對象引用的其它對象也進(jìn)行序列化。

標(biāo)簽: java
相關(guān)文章:
主站蜘蛛池模板: 成人69视频在线观看免费 | 黄色三级视频网站 | 亚洲精品午夜在线观看 | 成人毛片1024你懂的 | 久草小视频| 91久久精品国产亚洲 | 国产自在自线午夜精品视频在 | 波多野结衣在线视频观看 | 99亚洲精品| 深夜成人性视频免费看 | 国产普通话一二三道 | 欧美大陆日韩 | 无限观看社区在线视频 | 国产亚洲一区在线 | 成人午夜做爰视频免费看 | 三级视频网站在线观看 | 午夜影院啪啪 | 国产欧美亚洲三区久在线观看 | 欧美巨大精品欧美一区二区 | 久久久久久一级毛片免费无遮挡 | 朝鲜美女免费一级毛片 | 在线视频一区二区日韩国产 | 国模午夜写真福利视频在线 | 国产欧美日韩综合一区二区三区 | 欧美一级美片在线观看免费 | 国产成人一级片 | 日本αv | 日本aaa成人毛片 | 成年人免费在线观看网站 | 精品久久成人 | 成人欧美一区在线视频在线观看 | 国内自拍第100页 | 欧美在线bdsm调教一区 | 香蕉视频老司机 | 一级毛片区 | 怡红院免费全部视频在线 | 亚洲精品国产第一区二区三区 | 国产精品尹人在线观看免费 | 国产91色综合久久免费 | 欧美资源在线观看 | 欧美日韩一区二区综合在线视频 |