users@jersey.java.net

Re: [Jersey] Jersey-Spring and correct annotation

From: Chris Carrier <ctcarrier_at_gmail.com>
Date: Fri, 12 Feb 2010 09:12:06 -0800

Most of those annotations you ask about aren't really made any more
complicated by the Spring-Jersey interaction.

@Path, @Get, @Post etc are all Jersey annotations and whether you are
using Spring or not they work the same. The only thing to remember is
that if you are using the Spring Jersey servlet any Jersey resources
need to be registered as Spring beans or else they will probably get
ignored. That brings us to the Spring annotations you mention.

@Component simply tells Spring to register this class as a Spring bean
as long as you have Spring configured to scan for annotated classes
which you can do in the context.xml file like:

<context:component-scan base-package="com.something"/>

@Scope is a Spring annotation to allow you to set the scope of the
bean. The default is singleton but you can change it to always
instantiate a new instance. For my money most good spring beans are
stateless so should always be singletons (which is the default for
Spring) but others may disagree and anyway it's up to you.

I'm not familiar with @PerRequest. @Autowire tells Spring to try to
figure out the dependency injection at runtme. It can use either bean
names or bean types to try to guess though I don't remember what it
does by default. I've never really used it before as I do my wiring
in the context.xml file explicitly and don't like the idea of relying
on autowiring. @Inject is like explicitly wiring your DI in the
context file but using an annotation instead. Again I don't really
like this as it leads to classes that are 90% annotations and
impossible to read.

In general if you need to know more about the Spring annotations read
the Spring docs. They are really awesome. The best docs I've ever
worked with. They will explain these annotations better than I ever
will. For Jersey ones you can try the docs and examples or ask here.
I'm still learning Jersey myself and this list has been a great
resource.

Chris

On Fri, Feb 12, 2010 at 2:47 AM, Patrick Dreyer <Patrick_at_dreyer.name> wrote:
> Hi everyone
>
> The information and examples found about annotations related to
> Jersey-Spring are very confusing, as there is no source explaining when to
> use which annotation and why.
> Probably, most of the "confusion" comes out of the different versions of
> Jersey, Spring and Jersey-Spring. What I'm looking for is the correct use of
> annotations for
>
> * JRE 1.6.0_18-b07
> * Jersey 1.1.5
> * Spring 3.0.0-RELEASE
> * Jersey-Spring 1.1.5
>
> Examples I found specify Jersey resources like:
>
> @Path("...")
> @Component @Scope("request")
> public class RootResource { ... }
>
> Others use:
>
> @Path("...")
> @PerRequest
> @Component @Scope("request")
> public class RootResource { ... }
>
> Or even an other alternative found:
>
> @Path("...")
> @PerRequest
> public class RootResource { ... }
>
> Same goes for fields to be injected:
>
> @Inject private Task task;
>
> But others use:
>
> @Autowire private Task task;
>
>
> Regards
> ---
> Patrick Dreyer
>