从Jersey侦听器访问主类,而主类不是单例对象

时间:2014-08-06 07:48:08

标签: java jersey

我经常在我的Java应用程序中使用Jersey简单服务器作为Web服务。每当我这样做时,我似乎只能从我的应用程序(main类)获取属性,如果它被设置为单例。所以,我似乎只能用MainClass.getInstance()获取信息。

我知道我可以让Jersey监听器在Jersey中使用某种单例,但这只意味着侦听器对象将在Jersey中实例化一次而不是每一个请求:监听器不是真正的单例,因为它仍然需要一个public构造函数。

有没有办法访问我的应用程序类而不是单独的对象本身?

以下是相关代码:

public class JerseyService {

    private MainClass mainClass;
    private int port = 8080;
    private DefaultResourceConfig config;
    private Closeable jerseyServer;

    public JerseyService() {
        config = new DefaultResourceConfig(JerseyListener.class); // Attach the listener (resource) to the Jersey server
        jerseyServer = SimpleServerFactory.create("http://0.0.0.0:" + jerseyPort, config);
    }

    public String getData() {
        return "somedata";
    }

}

@Path("/")
public class JerseyListener {

    @Path("getData")
    @GET
    @Produces("application/json")
    public Response getData() {
        // How can i call getData() from JerseyService
        // without JerseyService being a singleton??
        return Response.status(Response.Status.OK).entity("{<jsondata>}").build();
    }

}

0 个答案:

没有答案
相关问题