What is serialization?
Serialization can save the state of our entity class, that is, save the state of the attributes in the current class to the hard disk to ensure that the attributes in this class can be used next time or in other services.
What should I pay attention to?
- Serialization only sequences the properties of objects, regardless of methods
- Static class members are not serialized because static attributes belong to classes. We serialize them according to the current object state
- The attribute modified by the transient keyword is not serialized
What scenarios need serialization?
- The object needs IO operation persistence
- Mybatis enables the L2 cache, and the readOnly attribute in the configuration file is false by default
- Pass the current entity class object in the distributed system
·The object needs IO operation persistence
First, let's look at the code example:
Entity class does not implement serialization interface
package Practice class demo.Why serialization.entity; /** * [Brief description]: * * @author zhihuan * @version 2021/6/12 11:31 * @see Practice demo Why serialization */ public class UserEntity { private String username; private String password; private String nickname; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getNickname() { return nickname; } public void setNickname(String nickname) { this.nickname = nickname; } @Override public String toString() { return "UserEntity{" + "username='" + username + '\'' + ", password='" + password + '\'' + ", nickname='" + nickname + '\'' + '}'; } }
Then we perform IO operations:
package Practice class demo.Why serialization; import org.omg.CORBA.portable.OutputStream; import Practice class demo.Why serialization.entity.UserEntity; import java.io.*; /** * [Brief description]: * * @author zhihuan * @version 2021/6/12 11:30 * @see Practice demo */ public class IOStreamTest { public static void main(String[] args) { File file = new File("UserEntity.ser"); try { ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(file)); UserEntity userEntity = new UserEntity(); userEntity.setNickname("Messi"); userEntity.setUsername("messi"); userEntity.setPassword("77777777"); objectOutputStream.writeObject(userEntity); objectOutputStream.close(); ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(file)); UserEntity tempUser =(UserEntity) objectInputStream.readObject(); System.out.println(tempUser.toString()); objectInputStream.close(); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } } }
You can see that the above exception without serialization has been reported when the object is written to the file.
After the entity class implements the serialization interface, it can be executed successfully!
Why do I / O operations on objects need to be persisted?
Here, I have another question when learning: why does JAVA not directly allow classes to be serialized, but need to implement an empty interface?? A reasonable explanation can be seen here: considering the security of JAVA, if there is no tag that allows developers to control the implementation of serialization, and each class can be serialized, the private attributes of some objects can be seen directly in the serialized file, which is still required to be private in some scenarios. Therefore, an empty interface is added to let developers decide the serialization problem. And this empty interface is just the class that marks and implements it can be serialized
·Mybatis enables the L2 cache, and the readOnly attribute in the configuration file is false by default
This subtitle is actually aimed at the scene of Mybatis, which is not general.
In fact, in the final analysis, it is a process of memory to hard disk persistence, because the caching mechanism in Mybatis is to put the queried content into memory first, and then if the memory is insufficient, the content in memory will be persisted to the hard disk, which is also a necessity for serialization as mentioned above.
Pass the current entity class object in the distributed system
Because the transmission of data in the network is binary, we need to serialize our object into a byte stream for transmission, and then get the object in the state we want to transfer through deserialization when the other party receives it
Question 1: if I don't use sequence words, can I convert them into JSON strings?
Of course, we can. Let's turn it into a string, and then let's take a look at the source code of string in Java. String will implement the serialization interface
Question 2: I remember that some VO classes have not been serialized. How can I still receive or send those request parameters?
This is because in that scenario, if we don't make specific changes, we use JSON serialization in most cases. Like JSON serialization, we are also familiar with many transcoding tool components such as fastjason provided by Ali, Gson of Google and MSON of meituan. What we are discussing here is only the serialization method provided by java. Don't confuse the two at present.
Rough opinion, if there is any mistake, please point out and discuss with the leaders passing by –