Jersey REST Server:实例化资源类 [英] Jersey REST Server: instantiating resource classes

查看:168
本文介绍了Jersey REST Server:实例化资源类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Jersey REST服务器上的教程[1]说明了Jersey servlet:

A tutorial on the Jersey REST server [1] says of the Jersey servlet:


这个servlet分析传入的HTTP请求并选择响应此请求的正确类和方法。此选择基于类和方法中的注释。

This servlet analyzes the incoming HTTP request and selects the correct class and method to respond to this request. This selection is based on annotations in the class and methods.

当servlet选择正确的类和方法时,是否重新选择 - 每次实例化课程?或者它是否保留每个资源类的一个实例?

When the servlet "selects the correct class and method", does it re-instantiate the class every time? Or does it keep one instance of every resource class?

这似乎很重要,因为如果这些资源类引用了存储应用程序范围状态的对象,这些状态对象将与资源一起重新实例化,并且不会最终正确地存储状态数据。

This would seem to matter because, if these resources classes have references to objects that store application-wide state, these state objects would be re-instantiated along with the resources, and would not end up storing state data correctly.

[1] http://www.vogella.com/articles/REST/article.html#restjersey

推荐答案

除非您使用 @Singleton 注释该类,否则Jersey将为每个请求建立每个类的新实例。

Jersey will establish a new instance of each class per request unless you annotate the class with @Singleton.

请参阅: https:// jersey.java.net/documentation/latest/jaxrs-resources.html#d0e2331

这篇关于Jersey REST Server:实例化资源类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆