users@jersey.java.net

Re: [Jersey] Spring, _at_Singleton resources and thread-safety

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Mon, 13 Oct 2008 18:22:34 +0200

On Oct 13, 2008, at 6:21 PM, Jeff Schmidt wrote:

> Hi Paul:
>
> Thanks for such a speedy response. :) A point of clarification is
> needed. Please below.
>
> Congrats on the 1.0 release! We appreciate your efforts.
>

Thanks.


>> You can inject ResourceContext:
>>
>> @Path("{id}")
>> public SubResource getSubResource(@Context ResourceContext rc) {
>> return rc.getResource(SubResource.class);
>> }
>>
>> Or you can do:
>>
>> @Path("{id}")
>> public Class<SubResource> getSubResource(@Context
>> ResourceContext rc) {
>> return SubResource.class;
>> }
>>
>> If you just obtain the Spring injected reference to SubResource
>> then Jersey will not perform injection onto the fields.
>
> Will the above cause Jersey to instantiate the sub resource class
> outside of Spring, or is this where the Spring prototype bean is
> instantiated and setup by Spring, and then Jersey obtains the
> reference and does its injection (e.g. in JerseySpringServlet)?

The latter, Jersey will defer to Spring to obtain the instance.


> How about if I return the Spring injected (singleton) reference to
> SubResource and code so I use only method parameter injection by
> Jersey (no fields or constructor params)? Will that work?

Yes.


> I assume Jersey can see those annotations in the existing
> SubResource instance and perform its injection?
>

Correct. In that situation you do not need to annotate the SubResource
with @Singleton. You only need to do that if you use the examples i
presented in my previous email.

Paul.