JavaEE Web JAX-RS:我可以在它的 class 中使用实例变量吗?
JavaEE Web JAX-RS: can i use instance variables inside it's class?
我正在寻找线程安全的 Servlet 替代方案,并且我找到了 JAX-RS 技术。
所以我可以像这样在 class 中使用实例变量吗(线程安全吗):
@Path("helloworld")
public class HelloWorldResource {
private String msg;
@GET
public void doSmth() {
this.msg = "test";
}
}
?
资源范围将默认为 @RequestScope
,因此每个请求都会创建一个新的资源实例。
来自Chapter 3. JAX-RS Application, Resources and Sub-Resources
@RequestScoped
Default lifecycle (applied when no annotation is present). In this scope the resource instance is created for each new request and used for processing of this request. If the resource is used more than one time in the request processing, always the same instance will be used. This can happen when a resource is a sub resource is returned more times during the matching. In this situation only on instance will server the requests.
所以只要 msg
不是 static
它就应该根据请求创建。
这也意味着在请求被处理后你将丢失资源中包含的任何状态,你在这里试图解决什么用例?
我正在寻找线程安全的 Servlet 替代方案,并且我找到了 JAX-RS 技术。
所以我可以像这样在 class 中使用实例变量吗(线程安全吗):
@Path("helloworld")
public class HelloWorldResource {
private String msg;
@GET
public void doSmth() {
this.msg = "test";
}
}
?
资源范围将默认为 @RequestScope
,因此每个请求都会创建一个新的资源实例。
来自Chapter 3. JAX-RS Application, Resources and Sub-Resources
@RequestScoped
Default lifecycle (applied when no annotation is present). In this scope the resource instance is created for each new request and used for processing of this request. If the resource is used more than one time in the request processing, always the same instance will be used. This can happen when a resource is a sub resource is returned more times during the matching. In this situation only on instance will server the requests.
所以只要 msg
不是 static
它就应该根据请求创建。
这也意味着在请求被处理后你将丢失资源中包含的任何状态,你在这里试图解决什么用例?