How to store the state of Java bean in a non web application?

187 Views Asked by At

As part of my RFT QA Automation, I have to store the state(Object) of Java bean and should access the object across the whole application, Singleton may not work here as it is not a web application, would you please suggest any other approach to store the state of java beans.

Appreciate your help.

Thanks

2

There are 2 best solutions below

3
Mumrah81 On

I'm not sure to understand your problem but why not:

  • serialize the objects to disk/database/...
  • use a static store (static singleton)
0
Michael Gantman On

Basically it is a typical Serialization/Deserialization issue. In general you need an ability to write the state of the class on to persistant layer and then to be able to restore the "same" instance from persistant layer. There is a built-in Java mechanizm that works through Serializable interface. However it is rarely used due to its inconvinience. One of te most popular ways to do this is to Serialize your class instances into JSON or XML format and then read it back and instanciate it from that format. If you need the Text to be human readable XML is preferred, otherwise JSON is the usual choice. The best tool to use for JSON serialization is JSON Jackson (FasterXML/jackson) Here are the relevant links: Json Jackson home. The main class that you will need to use is ObjectMapper. It is easy to use. Here is an article with example. Once you get a Json String of your instance write it into a file or DB and then read it and use ObjectMapper to -recreate your instance. It is simple and works very well