gpt4 book ai didi

java - RESTful Web 服务生命周期是 *always* 每个请求?

转载 作者:行者123 更新时间:2023-11-30 06:19:19 25 4
gpt4 key购买 nike

Beginning Java EE 7 by Antonio Goncalves它是这样写的:

编写 RESTful Web 服务 -> 生命周期和回调部分:

Thus, the life cycle of a RESTful web service is per request so the service does not have to worry about concurrency and can use instance variables safely.

然而几行之后他写道:

Remember that a RESTful web service can also be annotated with @Stateless or @Singleton to benefit from the services of session beans.

那是什么?仅每个请求的生命周期还是也可以是 session ?

除此之外,如果生命周期是 session ,它如何处理注入(inject)HttpHeaders到属性时的@Context注解? header 在 session 期间可能会有所不同,因此此属性是否始终在每次请求后更新?

最佳答案

根据 JAX-RS 规范,每个实现(如 Jersey)都必须支持每个请求的行为:

3.1.1 Lifecycle and Environment

By default a new resource class instance is created for each requestto that resource. First the constructor (see Section 3.1.2) is called,then any requested dependencies are injected (see Section 3.2), thenthe appropriate method (see Section 3.3) is invoked and finally theobject is made available for garbage collection.

An implementation MAY offer other resource class lifecycles, mechanisms for specifying theseare outside the scope of this specification. E.g. an implementationbased on an inversion-of-control framework may support all of thelifecycle options provided by that framework.

这意味着如果您的 Resource 类没有被额外注释,它是按请求的。一个众所周知的异常(exception)是 Spring bean:如果您的 Rest Resource 也是一个 Spring Bean,那么 Spring 会指定该类的生命周期。 EJB 也是如此 (@Singleton)

关于java - RESTful Web 服务生命周期是 *always* 每个请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23272905/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com