3

I have struts2-json-plugin lib in my project and I want to use JSONWriter to get JSON string and put it to JavaScript for one parameter only (not JSON result for the all Action) only one getter:

public String getCarTypesJson() throws JSONException {
        JSONWriter writer = new JSONWriter();
        return writer.write(carTypes);
}

Its work but can I skip creation of new JSONWriter() and inject JSONWriter object in my Action from Struts2?

Roman C
  • 49,761
  • 33
  • 66
  • 176
sytolk
  • 7,223
  • 3
  • 25
  • 38

1 Answers1

3

The Struts2 json plugin doesn't declare JSONWriter as an injectable bean. And inside the plugin JSONWriter is used exactly as you do it right now, by creating an instance of it each time there is a need to serialize something.

Of course you can create bean by yourself in some DI container and inject it into action. If you decide to do so make sure you declare the JSONWriter bean as prototype.

Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
  • 1
    Yes you are right. Maybe JSONWriter object is not so heavy if the serialize method in plugin lib create new instance of it each time. I have use in an other project json-simple lib and JSONObject was injected spring bean (with default singleton scope). – sytolk Sep 11 '15 at 07:15