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:01:18 -0600

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.

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.

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