Serialization of objects

This post explains how java objects can be serialized or deserialized in Spring framework

Spring framework provides two interfaces and their default implementations as mentioned below

Interfaces
1) Serializer
2) Deserializer

Default Implementations
1) DefaultSerializer
2) DefaultDeserializer

The default implementations internally uses java serialization to provide the serialization feature.

In other words these implementations act as wrapper around java serialization.

Below is an example of how to use them

Java Objects to Serialize


package Serializer;

import java.io.Serializable;

public class Person implements Serializable {
    private String name;
    private int age;
    private Address address;
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public int getAge() {
        return age;
    }
    
    public void setAge(int age) {
        this.age = age;
    }
    
    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    @Override
    public String toString() {
        String data = this.name + ":" + this.age + ":" + this.address;
        return data;
    }
}

package Serializer;

import java.io.Serializable;

public class Address implements Serializable {
    private String address1;
    private String address2;

    public String getAddress1() {
        return address1;
    }
    public void setAddress1(String address1) {
        this.address1 = address1;
    }
    public String getAddress2() {
        return address2;
    }
    public void setAddress2(String address2) {
        this.address2 = address2;
    }
    @Override
    public String toString() {
        String data = this.address1 + ":" + this.address2;
        return data;
    }
}

Main code


1  package Serializer;
2  
3  import java.io.File;
4  import java.io.FileInputStream;
5  import java.io.FileOutputStream;
6  import java.io.IOException;
7  
8  import org.springframework.context.ApplicationContext;
9  import org.springframework.context.support.ClassPathXmlApplicationContext;
10 import org.springframework.core.serializer.Deserializer;
11 import org.springframework.core.serializer.Serializer;
12 
13 public class SpringSerializerDemo1 {
14  public static void main(String[] args) throws Exception {
15      ApplicationContext context = new ClassPathXmlApplicationContext("serializer.xml");
16      
17      Serializer serializer = (Serializer)context.getBean("serializer");
18      Deserializer deserializer = (Deserializer)context.getBean("deSerializer");
19      
20      File file = new File("Person.ser");
21      
22      Person person1 = (Person)context.getBean("person1");    
23      
24      try(FileOutputStream fileOutputStream = new FileOutputStream(file)) {
25          serializer.serialize(person1, fileOutputStream);
26      } catch(IOException excep) {
27          excep.printStackTrace();
28      }
29      
30      Person person2 = null;
31      try(FileInputStream fileInputStream = new FileInputStream(file)) {
32          person2 = deserializer.deserialize(fileInputStream);
33      } catch(IOException excep) {
34          excep.printStackTrace();
35      }
36      
37      System.out.println(person2);
38  }
39 }

xml config file

Output

Jan 31, 2017 8:33:08 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5a10411: startup date [Tue Jan 31 20:33:08 EST 2017]; root of context hierarchy
Jan 31, 2017 8:33:08 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [serializer.xml]
name1:10:address1:address2

Explanation

As you can see from the config file. We create two bean declarations, one for DefaultSerializer and another for DefaultDeserializer at line 17 and 18 in config file.

We also create bean declaration of the object we want serializer in the config file from line 6 to 10 in the config file.

We load the config file at line 15 in the main code. We get an instance of DefaultSerializer and DefaultDeserializer by using their corresponding bean ids (which is “serializer” and “deSerializer”)
at line 17 and 18 in the main code.

We get an instance of Person instance using the bean id “person1”.

We call the serialize method of Serializer interface by passing the object to serialize and outputstream where it has to write it. Refer to lines from 24 to 28.

Next we call the deserialize method of Deserializer interface by passing the input stream from where it has to read the serialized object. It will return an instance of Person class.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s