繁体   English   中英

泽西岛春季一体化和范围

[英]Jersey Spring Integration and Scopes

我尝试进行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!";
   }
}

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

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

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

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

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

  • 使用@Scope(“ request”):您的bean的范围将是“ request”
  • 没有@Scope(“ request”):您的bean的范围将是“ singleton”(默认为spring)

无论您在Spring上使用CXF还是Jersey,它们都仅用于JAX-RS端点编程(不适用于管理bean)。

编辑 :我已经在文档中找到它:

由于端点是Spring @Component,因此其生命周期由Spring管理,您可以@Autowired依赖项并使用@Value注入外部配置。 默认情况下,Jersey servlet将被注册并映射到/ *。 您可以通过将@ApplicationPath添加到ResourceConfig来更改映射。

链接: 27.2 JAX-RS和Jersey

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM