- 浏览: 5193699 次
- 性别:
- 来自: 北京
-
文章分类
最新评论
-
silence19841230:
先拿走看看
SpringBoot2.0开发WebSocket应用完整示例 -
wallimn:
masuweng 写道发下源码下载地址吧!三个相关文件打了个包 ...
SpringBoot2.0开发WebSocket应用完整示例 -
masuweng:
发下源码下载地址吧!
SpringBoot2.0开发WebSocket应用完整示例 -
masuweng:
SpringBoot2.0开发WebSocket应用完整示例 -
wallimn:
水淼火 写道你好,我使用以后,图标不显示,应该怎么引用呢,谢谢 ...
前端框架iviewui使用示例之菜单+多Tab页布局
Be Careful With Transient Data
(原文来自http://www.devx.com/tips/Tip/13726
)
Expertise: Intermediate
Language: Java
January 28, 2000
Be Careful With Transient Data
Java's
serialization provides an elegant, and easy to use mechanism for making
an object's state persistent. While controlling object serialization,
we might have a particular object data member that we do not want the
serialization mechanism to save.
To turn off serialization on a
certain field of an object, we tag that field of the class of our
object with the Java's "transient" keyword. This, to low-level parts of
the Java virtual machine, is an indication that the transient variable
is not part of the persistent state of an object.
First, let's have some backgrounder code with Java's serialization.
Suppose we define a class as:
public class LoggingInfo implements java.io.Serializable
{
private Date loggingDate = new Date();
private String uid;
private transient String pwd;
LoggingInfo(String user, String password)
{
uid = user;
pwd = password;
}
public String toString()
{
String password=null;
if(pwd == null)
{
password = "NOT SET";
}
else
{
password = pwd;
}
return "logon info: \n " + "user: " + uid +
"\n logging date : " + loggingDate.toString() +
"\n password: " + password;
}
}
Now we can create an instance of this class and serialize it, and write the serialized object to disk as in:
LoggingInfo logInfo = new LoggingInfo("MIKE", "MECHANICS");
System.out.println(logInfo.toString());
try
{
ObjectOutputStream o = new ObjectOutputStream(
new FileOutputStream("logInfo.out"));
o.writeObject(logInfo);
o.close();
}
catch(Exception e) {//deal with exception}
To read the object back, we can write
try
{
ObjectInputStream in =new ObjectInputStream(
new FileInputStream("logInfo.out"));
LoggingInfo logInfo = (LoggingInfo)in.readObject();
System.out.println(logInfo.toString());
}
catch(Exception e) {//deal with exception}
If
we run this code, we notice that the read-back object prints password
as "NOT SET". This is exactly the effect we should have expected when
we declared the pwd field as transient.
Now, let's see a potential
problem that careless treatment of transient fields may cause. Suppose
we modify our class definition and provide default values for the
transient field, say we write:
public class GuestLoggingInfo implements java.io.Serializable
{
private Date loggingDate = new Date();
private String uid;
private transient String pwd;
GuestLoggingInfo()
{
uid = "guest";
pwd = "guest";
}
public String toString()
{
//same as above
}
}
Now,
if we serialize an instance of GuestLoggingInfo, write it to disk, and
read it back, we still see that the read-back object prints password as
"NOT SET". In effect, the process of reading back (de-serializing)
totally ignores the constructor of GuestLoggingInfo. So what happened?
The
answer lies in the fact that the initialization code is not called
because we are not initializing, in other words, we are not
constructing a brand new object, but loading back the persistent state
of an object of a class, and assigning that state to another object of
the same class. Declaring the pwd field as transient, excludes the data
for that field from the persistent state of our object. Then, upon
de-serialization, since there is no data preserved for the pwd field,
the field gets Java's default value for its type (null for String).
So, if you mark a field of an object as transient, and write that object to disk, expect to have the default value of the type of that field when you de-serialize the object, and not the actual value that the field had before its state was serialized. If a default value (or any meaningful value) is essential for a transient field of a de-serialized object, you have to assign it yourself either directly (if the field is public) or via a setter method.
Behrouz Fallahi
发表评论
-
gradle编译错误:Could not find method compile() for arguments
2020-09-19 10:50 18739编译(IDEA+Gradle)一个别人的工程,出现一个 ... -
netty心跳检查之UDP篇
2019-09-15 08:50 2551部分UDP通信场景中,需要客户端定期发送心跳信息,以获取终 ... -
解决tomcat部署两个SpringBoot应用提示InstanceAlreadyExistsException
2019-06-30 11:49 3576两个SpringBoot应用部署在一个Tomcat中,单独 ... -
Eclipse配置MyBatis代码自动化功能
2019-06-29 10:16 18981.安装插件 Eclipse中,Help->Ecli ... -
vue.js中使用qrcode生成二维码
2019-05-20 00:00 7722一、安装包 npm install qrcodejs2 --s ... -
MySQL插入数据报错: Incorrect string value: '\xFD\xDE'
2019-03-31 23:19 1302我MySQL数据库用的uft-8字符集,插入数据一直很正常 ... -
vue自定义组件并双向绑定属性
2019-03-08 22:46 3293做了两个子组件,原理基本一样,一个是使用原生的select ... -
vue-router简单示例
2019-03-05 00:32 1204写个基本完整、稍有借鉴意义的示例,防止自己忘记。 &l ... -
“联通充值系统繁忙”轻松应对
2019-02-06 11:03 4035大过年的,联通充个值一直报“充值系统繁忙”。昨天晚上试了几 ... -
electron.js数据库应用---导航菜单(element-ui+mysql)
2019-02-05 21:33 2424一、环境搭建 略, ... -
electron.js数据库应用---入门(mysql+element-ui)
2019-01-27 23:19 7595我的机器:Windows10,64 ... -
SpringMVC 在controller层中注入成员变量request,是否线程安全
2018-12-17 21:17 2816@RestController public class ... -
VueJS 组件参数名命名与组件属性转化
2018-12-03 00:00 2125转自:https://www.cnblogs.com/meiy ... -
vue-resource拦截器实现token发送及检验自动化
2018-11-16 22:38 3108用了很长时间vue-resource,最近思考$http发 ... -
element-ui试用手记
2018-10-29 20:25 1788element-ui、iviewui都以vue.js为基础 ... -
iviewui中表格控件中render的使用示例
2018-07-07 16:46 9823示例了如何在表格中显示按钮,如何将代码转化为文字。 i ... -
Tomcat错误“Alias name tomcat does not identify a key entry”解决
2018-07-05 21:39 6734申请到了阿里云的证书后,下载、按照说明生成jks格式证书、 ... -
阿里云免费证书“fileauth.txt内容配置错误”解决
2018-07-05 20:43 5376最近研究微信小程序开发,上阿里云申请了个证书,使用文件验证 ... -
springboot2.0跨域配置
2018-07-04 22:11 5316springboot2.0跨域配置: 一、代码 ... -
微信小程序使用code换openid的方法(JAVA、SpringBoot)
2018-07-01 21:52 10479微信小程序序的代码中提示,使用code换取openid,但 ...
相关推荐
Java序列化是Java平台中的一种持久化机制,它允许对象的状态被转换成字节流,以便存储、网络传输或在不同时间点恢复。这个过程被称为序列化,而反向操作称为反序列化。序列化在许多场景下都非常有用,比如在分布式...
Java序列化是Java平台中的一种标准机制,允许将对象的状态转换为字节流,以便存储在磁盘上、通过网络进行传输或者在某些时候恢复原来的对象状态。这一过程包括两个主要步骤:对象的序列化(将对象转换为字节流)和反...
在Java编程中,序列化是将对象的状态转换为字节流的过程,以便可以存储或在网络上传输。这个过程使得对象可以在不同的时间或者不同的系统之间进行恢复。然而,有时候我们不希望序列化对象的所有属性,可能是因为某些...
Java的序列化与反序列化是Java开发中的一项重要技术,它允许我们将对象的状态转换为字节流,以便存储或在网络上传输。`Serializable`接口是Java提供的一个标记接口,用于实现对象的序列化。当一个类实现了这个接口,...
下面是 10 个与 Java 序列化相关的面试题目: 1. 什么是 Java 序列化,如何实现 Java 序列化? Java 序列化是一种用来处理对象流的机制,所谓对象流也就是将对象的内容进行流化。为了实现 Java 序列化,需要将需要...
Java对象的序列化和反序列化是Java编程中一项重要的技术,主要用于将对象的状态转换为字节流,以便存储或在网络上传输。这一过程对于理解Java的IO操作、持久化数据以及实现分布式通信等场景非常关键。 首先,我们来...
Java序列化是Java平台中的一种核心机制,它允许对象的状态被转换成字节流,以便存储到磁盘、数据库,或者在网络中进行传输。这对于实现持久化、远程方法调用(RMI)以及Enterprise JavaBeans(EJB)等高级功能至关...
静态变量序列化 ---------------- 在 Java 序列化中,静态变量不会被序列化。这是因为静态变量是属于类的,而不是对象的。例如,清单 2 中,静态变量 staticVar 的值在序列化后被修改,但是读取对象时,静态变量的...
Java序列化是Java平台中的一种标准机制,它允许将对象的状态转换为字节流,以便存储、传输或恢复。在Java中,一个类如果要实现序列化,需要实现`Serializable`接口,这是一个标记接口,不包含任何方法。下面我们将...
#### 静态变量序列化 通常认为,静态变量不属于对象状态的一部分,因此不会被序列化。但在某些情况下,序列化机制会保存静态字段的值,这主要取决于序列化过程的具体实现。为了防止不必要的序列化,可以通过在类...
Java序列化是Java平台中的一种标准机制,允许对象的状态被保存到磁盘或者在网络中进行传输,以便在后续的时间或地点恢复这些对象。这个过程包括两个主要操作:序列化(将对象转换为字节流)和反序列化(将字节流恢复...
Java的序列化机制允许开发者在类中定义`readObject()`和`writeObject()`方法来自定义序列化和反序列化的行为,或者使用`transient`关键字来排除某些字段不参与序列化。 总之,Java序列化是一个强大的工具,它使得...
对于类中包含的transient和static修饰的成员变量,它们不会参与序列化过程,因为transient变量表示临时状态,static变量表示类级别的状态,不依赖于对象实例。 四、序列化过程 1. 创建ObjectOutputStream实例,如`...
深入理解Java虚拟机-Java内存区域透彻分析(序列化、反序列化概念及其使用场景+实现序列化的方式+transient关键字) Java序列化和反序列化是Java虚拟机中的一种重要机制,它们可以将Java对象转换为二进制数据,然后...
需要注意的是,即使`transient`变量不被序列化,但它的引用如果指向一个可序列化的对象,那么这个对象仍会被序列化。 总结来说,Java对象序列化是将对象状态转换为字节流,便于存储和网络传输的关键技术。通过实现`...
如果某些变量不希望被序列化,可以使用`transient`关键字标记它们。 - **安全性**:序列化可能导致安全问题,因为任何能读取序列化文件的人都可以构造对象并执行其方法。因此,应谨慎处理序列化数据,特别是在网络...
### Java对象序列化标准知识点详解 #### 一、系统架构概览 **1.1 概览** Java 对象序列化是一种将Java对象的状态转换成字节流的过程,以便于在网络上传输或存储到磁盘上。Java序列化标准定义了一套规则来描述如何...
Java序列化是将Java对象转换为字节流的过程,以便可以在磁盘、数据库或网络上存储或传输这些对象。这使得我们能够保存对象的状态,并在稍后的时间点恢复它,或者在网络之间传递对象。反序列化是相反的过程,即从字节...
Java对象序列化与反序列化是Java编程中重要的概念,主要应用于数据持久化、网络传输以及存储等场景。本文将详细解析这两个概念及其在实际应用中的实现方式。 **一、Java对象序列化** 1. **定义**: Java对象序列化...
Java transient关键字与序列化操作实例详解 Java语言中的transient关键字是用来修饰变量的,表明该变量不会被Java虚拟机(JVM)序列化,即不会被写入到二进制流中。序列化是Java语言中的一种机制,用于将对象转换为...