Q:

Serialize ClassPathXmlApplicationContext

I use spring with another frameworks nad I'm new in serialization. 
What the problem:
I need to serialize MyClass object that contains the org.eclipse.jetty.websocket.api.Session session (which is non-serializable). 
class MyClass {
  private org.eclipse.jetty.websocket.api.Session session; //NON-Serializable!

  private void writeObject(java.io.ObjectOutputStream out) throws IOException{
    out.writeObject(session);
  }

   private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
      session = (org.eclipse.jetty.websocket.api.Session) in.readObject();
   }
}

What the question:
I've read from here that it is possible to serialize object with non-serializable fields.  But, org.eclipse.jetty.websocket.api.Session has not-trivial class hierarchy. 
But when I try to doing so, it throw java.io.NotSerializableException: org.eclipse.jetty.websocket.common.WebSocketSession

A:

I think it would not make sense if ClassPathXmlApplicationContext was Serializable because then you would have to Serialize all objects created in this context together with the context. Instead you could save the application.xml and then read it from file and recreate ClassPathXmlApplicationContext from it. To prevent serializing context field you can make it transient as shamimz suggested or simply null it before serializing MyClass instance

