users@jersey.java.net

Re: [Jersey] Jersey-Spring and correct annotation

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Tue, 16 Feb 2010 12:36:11 +0100

On Feb 15, 2010, at 7:06 PM, Chris Carrier wrote:

> No I know of no examples. I was just trying to shed some light on
> Patrick's questions about the Spring-Jersey interaction because I
> found it a little confusing at first (especially when it came to
> creating Providers). I don't know that this is any shortcoming in the
> docs it's just always a bit confusing when using a bunch of different
> systems together.
>

I think it is the latter.

A Provider must always be in the scope of a singleton. When Jersey
manages the provider instance the scope is implied as singleton, just
like when Jersey manages a resource class instance the scope is
implied as per-request, unless otherwise explicitly stated.

When using a framework such as Guice, Spring or CDI you need to be
explicit about the scope using the scope-based annotations of those
frameworks.


> Though now that I think about it what happens if one does use both
> Jersey and Spring "scope" annotations within a Spring-Jersey app?
> Will the Jersey annotations just be ignored in favor of the Spring
> ones?

If the class is registered as a spring bean then the Spring-based
scope annotations will apply, otherwise the Jersey-based scope
annotations will apply. So it basically depends on what is doing the
instantiating.

It is very hard to detect such "forms" of redundancy so i am not sure
there s much we can do from the perspective of runtime warnings/errors.

Paul.

>
> Chris
>
> On Mon, Feb 15, 2010 at 2:51 AM, Paul Sandoz <Paul.Sandoz_at_sun.com>
> wrote:
>> Hi Chris,
>> On Feb 13, 2010, at 8:59 PM, Patrick Dreyer wrote:
>>
>> Thanks to Chris for the comprehensive answer.
>>
>> What I'm still confused about is the COMBINATION of the Jersey
>> annotations
>> @PerRequest and @Singleton with its Spring counterparts
>> @Scope("request")
>> and @Scope("singleton") (or simply omit it).
>> Are the Jersey ones required or the Spring ones, or even both?
>> This is my biggest struggle as there is no common sense in the
>> documentation
>> and examples I found.
>>
>>
>> You are right that such a combination does not make sense. If
>> Spring is used
>> the Jersey-based annotations are not required and will be ignored.
>> Can you point to the example/docs/code where such combinations are
>> declared?
>> Thanks,
>> Paul.
>>
>> Cheers
>> ----
>> Patrick Dreyer
>>
>> Chris Carrier schrieb:
>>
>> 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
>>
>>
>>
>> ---------------------------------------------------------------------
>> 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
>