users@jersey.java.net

Re: [Jersey] Having Jersey serve resource as singleton declared in applicationContext.xml

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Mon, 23 Feb 2009 16:04:18 +0100

On Feb 23, 2009, at 3:01 PM, Andrew Feller wrote:

> QUESTION: Is it possible to have Jersey serve a resource as a
> singleton yet declared within the applicationContext.xml?

Yes. (I presume you are referring to Spring managed bean that is a
resource class.)


> Is this even recommended?
>

Sure, if that is what you require. My preference is for per-request
because i think it is nicer to code to and safer but sometimes
singleton can make sense e.g. caching information in-memory on the
singleton.

The spring-annotations sample [1] uses the Spring singleton scope
(using annotations, not in the applicationContext.xml, but the result
is the same), for example:

@Path("/spring-autowired")
@Component
@Scope("singleton")
public class SpringAutowiredResource {

     @Autowired
     @Qualifier("1")
     private Item2 _item;

     @GET
     @Produces("application/xml")
     public Item2 getItem() {
         return _item;
     }
}

Paul.

[1] http://download.java.net/maven/2/com/sun/jersey/samples/spring-annotations/1.0.2/spring-annotations-1.0.2-project.zip