关于更多java序列化的问题可以参见JAVA系列之对象的序列化与反序列化
Java的序列化机制只序列化对象的属性值,而不会去序列化什么所谓的方法。其实这个问题简单思考一下就可以搞清楚,方法是不带状态的,就是一些指令,指令是不需要序列化的,只要你的JVM classloader可以load到这个类,那么类方法指令自然就可以获得。序列化真正需要保存的只是对象属性的值,和对象的类型。
我们可以做一个简单的小试验,来证实一下:
Java代码 < type="application/x-shockwave-flash" width="14" height="15" src="javascripts/syntaxhighlighter/clipboard_new.swf" pluginspage="http://www.macromedia.com/go/getflashplayer" allowscriptaccess="always" quality="high" flashvars="clipboard=package%20com.javaeye%3B%0A%0Aimport%20java.io.Serializable%3B%0A%0Apublic%20class%20DomainObject%20%20implements%20Serializable%20%7B%0A%0A%09private%20String%20name%3B%0A%09%0A%09private%20int%20age%20%3B%0A%0A%09public%20int%20getAge()%3B%20%7B%0A%09%09return%20age%3B%0A%09%7D%0A%0A%09public%20void%20setAge(int%20age)%3B%20%7B%0A%09%09this.age%20%3D%20age%3B%0A%09%7D%0A%0A%09public%20String%20getName()%3B%20%7B%0A%09%09return%20name%3B%0A%09%7D%0A%0A%09public%20void%20setName(String%20name)%3B%20%7B%0A%09%09this.name%20%3D%20name%3B%0A%09%7D%0A%09%0A%09%0A%7D">
package com.javaeye;
import java.io.Serializable;
public class DomainObject implements Serializable {
private String name;
private int age ;
public int getAge(); {
return age;
}
public void setAge(int age); {
this.age = age;
}
public String getName(); {
return name;
}
public void setName(String name); {
this.name = name;
}
}view plaincopy to clipboardprint?
package com.javaeye;
import java.io.Serializable;
public class DomainObject implements Serializable {
private String name;
private int age ;
public int getAge(); {
return age;
}
public void setAge(int age); {
this.age = age;
}
public String getName(); {
return name;
}
public void setName(String name); {
this.name = name;
}
}
package com.javaeye;
import java.io.Serializable;
public class DomainObject implements Serializable {
private String name;
private int age ;
public int getAge(); {
return age;
}
public void setAge(int age); {
this.age = age;
}
public String getName(); {
return name;
}
public void setName(String name); {
this.name = name;
}
}
Java代码
package com.javaeye;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
public class Main {
public static void main(String[] args); throws Exception {
DomainObject obj = new DomainObject();;
obj.setAge(29);;
obj.setName("fankai");;
FileOutputStream fos = new FileOutputStream("DomainObject");;
ObjectOutputStream oos = new ObjectOutputStream(fos);;
oos.writeObject(obj);;
oos.close();;
fos.close();;
}
}view plaincopy to clipboardprint?
package com.javaeye;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
public class Main {
public static void main(String[] args); throws Exception {
DomainObject obj = new DomainObject();;
obj.setAge(29);;
obj.setName("fankai");;
FileOutputStream fos = new FileOutputStream("DomainObject");;
ObjectOutputStream oos = new ObjectOutputStream(fos);;
oos.writeObject(obj);;
oos.close();;
fos.close();;
}
}
package com.javaeye;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
public class Main {
public static void main(String[] args); throws Exception {
DomainObject obj = new DomainObject();;
obj.setAge(29);;
obj.setName("fankai");;
FileOutputStream fos = new FileOutputStream("DomainObject");;
ObjectOutputStream oos = new ObjectOutputStream(fos);;
oos.writeObject(obj);;
oos.close();;
fos.close();;
}
}
DomainObject是我们准备序列化的类,在Main里面,我们new一个DomainObject的对象,然后赋值,最后把该对象序列化到一个硬盘文件中。然后使用一种支持二进制编辑器,例如UltraEdit打开这个文件,看看Java都对DomainObject序列化了哪些信息,你就什么都明白了。
为了更方便观察,我使用Linux下面的strings去提取文本信息,输出为:
robbin@linux:~> strings DomainObject
com.javaeye.DomainObject
ageL
namet
Ljava/lang/String;xp
fankai
这些信息很直观的告诉我们序列化都保存了些什么内容:
1)对象的类型
2)对象属性的类型
3)对象属性的值
并没有什么方法签名的信息,更不要说什么序列化方法了。
然后我们再做一个试验,给DomainObject增加两个方法:
Java代码 < type="application/x-shockwave-flash" width="14" height="15" src="javascripts/syntaxhighlighter/clipboard_new.swf" pluginspage="http://www.macromedia.com/go/getflashplayer" allowscriptaccess="always" quality="high" flashvars="clipboard=package%20com.javaeye%3B%0A%0Aimport%20java.io.Serializable%3B%0A%0Apublic%20class%20DomainObject%20%20implements%20Serializable%20%7B%0A%0A%09private%20String%20name%3B%0A%09%0A%09private%20int%20age%20%3B%0A%0A%09public%20int%20getAge()%3B%20%7B%0A%09%09return%20age%3B%0A%09%7D%0A%0A%09public%20void%20setAge(int%20age)%3B%20%7B%0A%09%09this.age%20%3D%20age%3B%0A%09%7D%0A%0A%09public%20String%20getName()%3B%20%7B%0A%09%09return%20name%3B%0A%09%7D%0A%0A%09public%20void%20setName(String%20name)%3B%20%7B%0A%09%09this.name%20%3D%20name%3B%0A%09%7D%0A%09%0A%09public%20String%20toString()%3B%20%7B%0A%09%09return%20%22This%20is%20a%20serializable%20test!%22%3B%0A%09%7D%0A%09%0A%09public%20void%20doSomeWork()%3B%20%7B%0A%09%09System.out.println(%22hello%22)%3B%3B%0A%09%7D%0A%7D">
package com.javaeye;
import java.io.Serializable;
public class DomainObject implements Serializable {
private String name;
private int age ;
public int getAge(); {
return age;
}
public void setAge(int age); {
this.age = age;
}
public String getName(); {
return name;
}
public void setName(String name); {
this.name = name;
}
public String toString(); {
return "This is a serializable test!";
}
public void doSomeWork(); {
System.out.println("hello");;
}
}view plaincopy to clipboardprint?
package com.javaeye;
import java.io.Serializable;
public class DomainObject implements Serializable {
private String name;
private int age ;
public int getAge(); {
return age;
}
public void setAge(int age); {
this.age = age;
}
public String getName(); {
return name;
}
public void setName(String name); {
this.name = name;
}
public String toString(); {
return "This is a serializable test!";
}
public void doSomeWork(); {
System.out.println("hello");;
}
}
package com.javaeye;
import java.io.Serializable;
public class DomainObject implements Serializable {
private String name;
private int age ;
public int getAge(); {
return age;
}
public void setAge(int age); {
this.age = age;
}
public String getName(); {
return name;
}
public void setName(String name); {
this.name = name;
}
public String toString(); {
return "This is a serializable test!";
}
public void doSomeWork(); {
System.out.println("hello");;
}
}
我们增加了toString方法和doSomeWork方法,按照你的理论,如果序列化方法的话,产生的文件体积必然增大。记录一下文件体积,92Byte,好了,删除,运行程序,生成了新的文件,看一下体积,还是92Byte!
拿到Linux下面再提取一下字符串:
robbin@linux:~> strings DomainObject
com.javaeye.DomainObject
ageL
namet
Ljava/lang/String;xp
fankai
完全一模一样!
然后我们再做第三个试验,这次把DomainObject的两个属性以及相关方法删除掉:
Java代码 < type="application/x-shockwave-flash" width="14" height="15" src="javascripts/syntaxhighlighter/clipboard_new.swf" pluginspage="http://www.macromedia.com/go/getflashplayer" allowscriptaccess="always" quality="high" flashvars="clipboard=package%20com.javaeye%3B%0A%0Aimport%20java.io.Serializable%3B%0A%0Apublic%20class%20DomainObject%20%20implements%20Serializable%20%7B%0A%0A%09public%20String%20toString()%3B%20%7B%0A%09%09return%20%22This%20is%20a%20serializable%20test!%22%3B%0A%09%7D%0A%09%0A%09public%20void%20doSomeWork()%3B%20%7B%0A%09%09System.out.println(%22hello%22)%3B%3B%0A%09%7D%0A%7D">
package com.javaeye;
import java.io.Serializable;
public class DomainObject implements Serializable {
public String toString(); {
return "This is a serializable test!";
}
public void doSomeWork(); {
System.out.println("hello");;
}
}view plaincopy to clipboardprint?
package com.javaeye;
import java.io.Serializable;
public class DomainObject implements Serializable {
public String toString(); {
return "This is a serializable test!";
}
public void doSomeWork(); {
System.out.println("hello");;
}
}
package com.javaeye;
import java.io.Serializable;
public class DomainObject implements Serializable {
public String toString(); {
return "This is a serializable test!";
}
public void doSomeWork(); {
System.out.println("hello");;
}
}
修改Main类如下:
Java代码 < type="application/x-shockwave-flash" width="14" height="15" src="javascripts/syntaxhighlighter/clipboard_new.swf" pluginspage="http://www.macromedia.com/go/getflashplayer" allowscriptaccess="always" quality="high" flashvars="clipboard=package%20com.javaeye%3B%0A%0Aimport%20java.io.FileOutputStream%3B%0Aimport%20java.io.ObjectOutputStream%3B%0A%0Apublic%20class%20Main%20%7B%0A%0A%09public%20static%20void%20main(String%5B%5D%20args)%3B%20throws%20Exception%20%7B%0A%09%09DomainObject%20obj%20%3D%20new%20DomainObject()%3B%3B%0A%0A%09%09FileOutputStream%20fos%20%3D%20new%20FileOutputStream(%22DomainObject%22)%3B%3B%0A%09%09ObjectOutputStream%20oos%20%3D%20new%20ObjectOutputStream(fos)%3B%3B%0A%09%09oos.writeObject(obj)%3B%3B%0A%09%09oos.close()%3B%3B%0A%09%09fos.close()%3B%3B%0A%09%7D%0A%0A%7D">
package com.javaeye;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
public class Main {
public static void main(String[] args); throws Exception {
DomainObject obj = new DomainObject();;
FileOutputStream fos = new FileOutputStream("DomainObject");;
ObjectOutputStream oos = new ObjectOutputStream(fos);;
oos.writeObject(obj);;
oos.close();;
fos.close();;
}
}view plaincopy to clipboardprint?
package com.javaeye;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
public class Main {
public static void main(String[] args); throws Exception {
DomainObject obj = new DomainObject();;
FileOutputStream fos = new FileOutputStream("DomainObject");;
ObjectOutputStream oos = new ObjectOutputStream(fos);;
oos.writeObject(obj);;
oos.close();;
fos.close();;
}
}
package com.javaeye;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
public class Main {
public static void main(String[] args); throws Exception {
DomainObject obj = new DomainObject();;
FileOutputStream fos = new FileOutputStream("DomainObject");;
ObjectOutputStream oos = new ObjectOutputStream(fos);;
oos.writeObject(obj);;
oos.close();;
fos.close();;
}
}
按照你的理论,如果序列化方法的话,我们必然应该在文件里面发现方法的签名信息,甚至方法里面包含的字符串,好了,再运行一遍,然后打开看一下吧!文件现在体积变成了45Byte,拿到Linux下面提取一下信息:
robbin@linux:~> strings DomainObject
com.javaeye.DomainObject
只有对象的类型信息,再无其它东西了!
请记住序列化机制只保存对象的类型信息,属性的类型信息和属性值,和方法没有什么关系,你就是给这个类增加10000个方法,序列化内容也不会增加任何东西,不要想当然的臆测自己不了解的知识,动手去做!
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/ilibaba/archive/2009/03/10/3975680.aspx