亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

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

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

瀏覽:105日期: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
相關文章:
主站蜘蛛池模板: 国产黄拍| 日韩高清特级特黄毛片 | 国产呦精品一区二区三区网站 | 日本免费大片免费视频 | 国产欧美自拍 | 91在线视频免费 | 亚洲丝袜在线播放 | 在线亚洲精品防屏蔽 | 精品国产欧美sv在线观看 | 91精品手机国产免费 | 免费一级视频在线播放 | 日韩中文字幕在线 | 欧美综合激情网 | 欧美第二区 | 成人免费动作大片黄在线 | 国产a∨一区二区三区香蕉小说 | 久久久香蕉视频 | 人人狠狠综合久久亚洲 | 青青青国产免费手机视频在线观看 | 永久精品免费影院在线观看网站 | 黄污视频免费观看 | 精字窝地址二永久2021 | 青青操免费在线观看 | 亚洲香蕉视频 | 日本黄色二级片 | 免费黄毛片 | 在线欧美v日韩v国产精品v | 婷婷丁香五 | 亚洲美女综合网 | 在线国产欧美 | 亚洲欧美国产精品 | 免费黄色三级 | 国产黄色片在线免费观看 | 欧美一级乱理片免费观看 | 久久网国产 | 国产精品黄在线观看免费软件 | 五月一区二区久久综合天堂 | 精品日韩欧美一区二区三区在线播放 | 韩国无遮挡三级伦在线大全 | 香蕉久久一区二区三区 | 国产一级高清免费观看 |