users@jersey.java.net

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

From: Andrew Feller <afelle1_at_lsu.edu>
Date: Mon, 23 Feb 2009 11:53:24 -0600

Let me see if I can rephrase the question:

Is there a way to declare a Spring bean that represents a Jersey resource
within the Spring XML file (applicationContext.xml) and have this Spring
bean used to process requests?

<bean id="blahResource" class="org.example.rest.resources.BlahResource">
    <property name="..." value="..." />
    <property name="..." value="..." />
</bean>


On 2/23/09 11:10 AM, "Paul Sandoz" <Paul.Sandoz_at_Sun.COM> wrote:

>
> On Feb 23, 2009, at 6:01 PM, Andrew Feller wrote:
>
>> Good morning Paul,
>>
>> So let me see if I understand this correctly (it is Monday):
>>
>> 1. The @Scope('singleton') annotation will tell the Jersey
>> SpringServlet
>> that this resource should only be instantiated once.
>>
>> 2. The @Component annotation will tell the Jersey SpringServlet that
>> this
>> resource should be looked up within the contextConfigLocation file
>> as
>> noted in web.xml.
>>
>
> The above annotations are defined by the Spring API. They declare to
> Spring what the scope of the Spring bean is and that it is identified
> as a Spring bean. You could do the same in the Spring XML
> configuration if you wished.
>
> The Jersey Spring integration defers to Spring for to obtain an
> instance. Jersey determines the Spring scope under which Spring
> instantiates so it can inject JAX-RS/Jersey specific stuff within the
> constraints of the Spring scope.
>
>
>> 3. If I follow the previous two assumptions, then I wouldn't need to
>> annotate resource fields as @Resource in order to inject objects
>> into it; I could simply inject them while declaring the resource.
>>
>
> I am not sure i understand you. You mean in the constructor as
> constructor parameters?
>
> Paul.
>
>> Thanks once again for the help!
>> Andrew
>>
>>
>> On 2/23/09 9:04 AM, "Paul Sandoz" <Paul.Sandoz_at_Sun.COM> wrote:
>>
>>>
>>> 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
>>>
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>>
>>
>> --
>> Andrew Feller, Analyst
>> LSU University Information Services
>> 200 Frey Computing Services Center
>> Baton Rouge, LA 70803
>> Office: 225.578.3737
>> Fax: 225.578.6400
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>

-- 
Andrew Feller, Analyst
LSU University Information Services
200 Frey Computing Services Center
Baton Rouge, LA 70803
Office: 225.578.3737
Fax: 225.578.6400