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 19:01:34 +0100

On Feb 23, 2009, at 6:53 PM, Andrew Feller wrote:

> 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>
>

I understand now. Yes you can, at the end of the email is a 2.5
compliant applicationContext.xml we use for testing.

Paul.

<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:util="http://www.springframework.org/schema/util"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
">

     <!-- Autowiring support (spring 2.5) -->
     <!--
         The AutowiredAnnotationBeanPostProcessor and
CommonAnnotationBeanPostProcessor are both
         included implicitly when using the component-scan element.
         That means that the two components are autodetected and
wired together
     -->
     <context:component-scan base-package="com.sun.jersey.spring25"/>

     <aop:aspectj-autoproxy/>

     <bean id="loggingAdvice"
class="com.sun.jersey.spring25.LoggingAdvice" />

     <bean id="managedSingletonResource"
class="com.sun.jersey.spring.SpringManagedSingletonResource"
         scope="singleton">
         <property name="name">
             <util:constant static-
field="com.sun.jersey.spring.TestData.MANAGED" />
         </property>
         <property name="item" ref="singletonItem" />
     </bean>

     <bean id="managedSingletonResourceWithConstructor"
class
="com.sun.jersey.spring.SpringManagedSingletonResourceWithConstructor"
         scope="singleton">
         <constructor-arg>
             <util:constant static-
field="com.sun.jersey.spring.TestData.MANAGED" />
         </constructor-arg>
         <constructor-arg ref="singletonItem" />
     </bean>

     <bean id="managedPerRequestResource"
class="com.sun.jersey.spring.SpringManagedPerRequestResource"
         scope="prototype">
         <property name="singletonItem" ref="singletonItem" />
         <property name="prototypeItem" ref="prototypeItem" />
     </bean>

     <bean id="managedPerRequestResourceWithConstructor"
class
="com.sun.jersey.spring.SpringManagedPerRequestResourceWithConstructor"
         scope="prototype">
         <constructor-arg ref="singletonItem" />
         <constructor-arg ref="prototypeItem" />
     </bean>

     <bean id="singletonItem" class="com.sun.jersey.spring.Item"
scope="singleton">
         <property name="value">
             <util:constant static-
field="com.sun.jersey.spring.TestData.MANAGED" />
         </property>
     </bean>

     <bean id="prototypeItem" class="com.sun.jersey.spring.Item2"
scope="prototype">
         <property name="value">
             <util:constant static-
field="com.sun.jersey.spring.TestData.MANAGED" />
         </property>
     </bean>

     <bean id="namedItem3_1" class="com.sun.jersey.spring.Item3"
scope="singleton">
         <property name="value">
             <util:constant static-
field="com.sun.jersey.spring.TestData.VAL_1" />
         </property>
     </bean>

     <bean id="namedItem3_2" class="com.sun.jersey.spring.Item3"
scope="singleton">
         <property name="value">
             <util:constant static-
field="com.sun.jersey.spring.TestData.VAL_2" />
         </property>
     </bean>

</beans>

>
> 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
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>