users@jersey.java.net

Re: Resource Lifecycle, Providers and Content-Length

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Thu, 10 Apr 2008 12:29:26 +0200

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.

Paul.

-- 
| ? + ? = To question
----------------\
    Paul Sandoz
         x38109
+33-4-76188109