【问题标题】:Jersey Spring Integration and ScopesJersey Spring 集成和范围
【发布时间】:2017-01-12 15:36:38
【问题描述】:

我尝试了 Spring 和 Jersey 集成。但我对范围感到困惑。 对于 spring,默认范围是 Singleton。 泽西岛的默认范围是请求。

例如:

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

 // The Java class will be hosted at the URI path "/myresource"
 @Path("/myresource")
 @Component
 @Scope("request")

 public class MyResource {

   // The Java method will process HTTP GET requests
   @GET
   // The Java method will produce content identified by the MIME Media
   // type "text/plain"
   @Produces("text/plain")
   public String getIt() {
       return "Hi there!";
   }
}

组件注解使类成为 Spring Bean。Spring bean 是默认的 Singleton,jersey 是默认的请求范围。

问题来了:这个 bean 的作用域是什么。

  • 如果我输入 @Scope("request") 是否会成为“请求范围”。

  • 如果我不输入 @Scope("request") 实际范围是什么?

【问题讨论】:

    标签: java spring jersey jax-rs jersey-2.0


    【解决方案1】:

    如您所说,您将MyResource 设为 Spring Bean,因此作用域由 Spring 处理。

    • 使用 @Scope("request") :您的 bean 的范围将是“请求”
    • 如果没有 @Scope("request") :你的 bean 的范围将是“singleton”(spring 默认)

    无论您是使用 CXF 还是带有 Spring 的 Jersey,这些仅用于 JAX-RS 端点编程(不用于管理 bean)。

    编辑:我在文档中找到了它:

    由于 Endpoint 是 Spring @Component,它的生命周期由 Spring,您可以 @Autowired 依赖项并注入外部 使用@Value 进行配置。 Jersey servlet 将被注册并 默认映射到 /*。您可以通过添加更改映射 @ApplicationPath 到你的 ResourceConfig。

    链接:27.2 JAX-RS and Jersey

    【讨论】:

      猜你喜欢
      • 2014-10-28
      • 2017-05-24
      • 1970-01-01
      • 1970-01-01
      • 2015-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-10
      相关资源
      最近更新 更多