struts2-json-plugin JSONWriter 注入实战
struts2-json-plugin JSONWriter Injection in Action
我的项目中有 struts2-json-插件库,我想使用 JSONWriter 获取 JSON 字符串并将其放入 JavaScript 仅用于一个参数(不是 JSON 所有操作的结果)只有一个 getter:
public String getCarTypesJson() throws JSONException {
JSONWriter writer = new JSONWriter();
return writer.write(carTypes);
}
它的工作,但我可以跳过 new JSONWriter()
的创建并从 Struts2 在我的 Action 中注入 JSONWriter
对象吗?
每次需要序列化的时候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。
当然你可以自己在一些DI容器中创建bean并注入到action中。如果您决定这样做,请确保将 JSONWriter
bean 声明为 prototype
.
我的项目中有 struts2-json-插件库,我想使用 JSONWriter 获取 JSON 字符串并将其放入 JavaScript 仅用于一个参数(不是 JSON 所有操作的结果)只有一个 getter:
public String getCarTypesJson() throws JSONException {
JSONWriter writer = new JSONWriter();
return writer.write(carTypes);
}
它的工作,但我可以跳过 new JSONWriter()
的创建并从 Struts2 在我的 Action 中注入 JSONWriter
对象吗?
每次需要序列化的时候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。
当然你可以自己在一些DI容器中创建bean并注入到action中。如果您决定这样做,请确保将 JSONWriter
bean 声明为 prototype
.