gpt4 book ai didi

java - 请求范围的 ContainerRequestFilter

转载 作者:行者123 更新时间:2023-11-30 09:03:59 34 4
gpt4 key购买 nike

我有一个 ContainerRequestFilter 实现,例如HeaderRequestFilter,我想让它为每个请求实例化。我正在使用 Spring 和 Jersey 。我尝试将 @Scope("request") 放在 HeaderRequestFilter 之上,但它会被实例化一次,即单例。我想维护像 javax.ws.rs.core.UriInfojavax.ws.rs.core.Request 这样的状态。所以如果这个类是单例的,那么它将受到竞争条件的影响。

有人可以帮我解决这个问题吗?谢谢。

最佳答案

来自JAX-RS specification, 6.4 :

By default, just like all the other providers, a single instance of each filter or entity interceptor is instantiated for each JAX-RS application. (...) Implementations MAY offer alternative lifecycle options beyond the default one.

我不知道是否有任何实现提供了不同的生命周期。

例如,您无需担心竞争条件。通过 @Context 注入(inject) HttpServletRequest。规范在第 9.1 章中明确指出:

Context is specific to a particular request but instances of certain JAX-RS components (providers and resource classes with a lifecycle other than per-request) may need to support multiple concurrent requests. When injecting an instance of one of the types listed in Section 9.2, the instance supplied MUST be capable of selecting the correct context for a particular request. Use of a thread-local proxy is a common way to achieve this.

关于java - 请求范围的 ContainerRequestFilter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25405065/

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