Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Jersey Spring集成和范围_Java_Spring_Jersey_Jax Rs_Jersey 2.0 - Fatal编程技术网

Java Jersey Spring集成和范围

Java Jersey Spring集成和范围,java,spring,jersey,jax-rs,jersey-2.0,Java,Spring,Jersey,Jax Rs,Jersey 2.0,我尝试了Spring和Jersey的集成,但我对范围感到困惑。 对于spring,默认范围是Singleton。 对于Jersey,默认范围是Request 例如: import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; // The Java class will be hosted at the URI path "/myresource"

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

例如:

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!";
   }
}
组件注释使类成为Springbean。Springbean是默认的单例,jersey是默认的请求范围

问题是:这个bean的范围是什么

  • 如果我把@Scope(“request”)放进去,它是否会变成“request Scope”

  • 如果我不放@Scope(“request”),那么实际的作用域是什么


正如您所说,您将
MyResource
作为Springbean,因此范围由Spring处理

  • 使用@Scope(“request”):bean的作用域将是“request”
  • 没有@Scope(“request”):bean的作用域将是“singleton”(spring默认值)
无论您在Spring中使用CXF还是Jersey,它们都只用于JAX-RS端点编程(不用于管理bean)

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

由于端点是Spring@组件,其生命周期由 Spring,您可以@Autowired dependencies并注入外部 配置为@Value。将注册Jersey servlet并 默认情况下映射到/*。您可以通过添加 @资源配置的应用程序路径

链接: