0
在我们公司,我们使用Windows中的小型Java应用程序作为服务工作了很多。为了能够从这些应用程序获取状态报告,我们使用Jersey来输出一些JSON数据。如何从Jersey资源访问非单体对象?
为了获得所需的应用程序数据,我们目前将应用程序设置为单例。从泽西岛的资源处理程序中,我们可以通过它的静态getInstance方法访问该对象。
现在我们正在升级完整的应用程序环境,并对我们的应用程序进行了一些更改。其中一个变化是应用程序不再是单身人士。是否有任何其他方式访问应用程序对象,而不是单身人士和没有作为内部类的处理程序?
下面是代码的简化版本:
public class Main {
protected int data; // a property which has to be accessible by
// the jersey handler
protected Closeable server;
protected ResourceConfig resourceConfig;
public Main() {
// set the jersey handle
resourceConfig = new DefaultResourceConfig(JerseyHandler.class);
// start the jersey server
server = SimpleServerFactory.create("http://0.0.0.0:" + port, resourceConfig);
}
public int getData() {
return data;
}
}
@Path("/")
public class JerseyHandler {
@Path("status")
@GET
public Response status() {
// how to access Main's getData() method from here without
// anything being a singleton or an inner class???
int data = ????;
Response.ok().entity(data).build();
}
}
这确实是什么样子我需要。谢谢!最后一个问题:你不需要声明应该注入哪个LanguageService实例的地方吗? – 2014-09-04 07:45:03
@RickBrunken:我被打断了。我更新了我的答案。当然,你应该提供你的Guice配置,我描述了我是如何为我的服务器做的 – nogard 2014-09-04 08:45:28