users@jersey.java.net

Re: Resource Lifecycle, Providers and Content-Length

From: Jean Aurambault <aurambaj_at_yahoo-inc.com>
Date: Fri, 11 Apr 2008 10:32:13 +0200

Paul Sandoz wrote:
> Martin Grotzke wrote:
>>> Here is an example:
>>>
>>> @Path("/test/")
>>> public class TestResource {
>>>
>>> private static Logger logger =
>>> Logger.getLogger(TestResource.class.getName());
>>> @Context
>>> private UriInfo uriInfo;
>>> private ProductService productService = null;
>>> public void setProductService(ProductService productService) {
>>> this.productService = productService;
>>> }
>>> public TestResource() {
>>> logger.debug("TestResource Constructor");
>>> }
>>> @GET
>>> @Path("coucou/")
>>> public String getCoucou() {
>>> return "coucou"; }
>>> }
>>>
>>> The problem seems to be related to spring. As soon as spring is used
>>> to inject the productService, the lifecycle doesn't work any more
>>> (behave like @Singleton). I use the spring servlet you have written
>>> in a previous article. I don't know where I should start the
>>> investigation, maybe you have some tips?
>> I'm not sure if this is the same issue I ran into, but perhaps this is
>> the case.
>>
>> If the default constructor of your resource is seen as a constructor by
>> jersey, then the PerRequestProvider invokes
>>
>> ComponentProvider.getInstance(Scope, Constructor, Object[])
>>
>> which returns null in the original example of the
>> SpringComponentProvider at [1].
>
> Jersey will only use the above method if there are one or more
> constructor parameters. In this case there are no constructor
> parameters so it should be going through the correct call path.
>
> What is the scope of the TestResource in the applicationContext.xml,
> "singleton" or "prototype" ?
>
> In my example i have working per-request managed Spring resources
> (only with an empty constructor) but i had to set the scope to
> "prototype". For the PerRequestResource the name value was always set
> correctly and the other field value was always reset.
>

My mistake, I hadn't defined the scope correctly in the
applicationContext.xml... So I don't need to redefine the getInstance()
as you mentioned Martin.
I have noticed weird behavior (eg. two instanciation instead of one)
when mixing jersey annotation (@PerRequest, @Singleton) and spring scope
(prototype, singleton). Unfortunately, I haven't time right now (I have
to deliver ;-) ...) to play with it and try Martin's branch.

Jean.

> Paul.
>