简体   繁体   中英

Jersey Spring Integration and Scopes

I try Spring and Jersey integration.But I'm confused about scopes. For spring the default scope is Singleton. And for Jersey default scope is Request.

For example:

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!";
   }
}

Component annotation makes the class a Spring Bean.Spring bean is default Singleton and jersey is default request scope.

Here is the question: What is the scope for this bean.

  • If I put @Scope("request") does it make "request scope".

  • And if I don't put @Scope("request") what is the actual scope?

As you said, you make MyResource a Spring Bean, so the scope is handled by Spring.

  • With @Scope("request") : the scope of your bean will be "request"
  • Without @Scope("request") : the scope of your bean will be "singleton" (spring default)

Whether you use CXF or Jersey with Spring, these are only used for the JAX-RS endpoints programming (not for managing beans).

EDIT : I've found it in the documentation:

Since the Endpoint is a Spring @Component its lifecycle is managed by Spring and you can @Autowired dependencies and inject external configuration with @Value. The Jersey servlet will be registered and mapped to /* by default. You can change the mapping by adding @ApplicationPath to your ResourceConfig.

link: 27.2 JAX-RS and Jersey

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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