users@jersey.java.net

[Jersey] Jersey 2.2 Spring Integration issues

From: Daniel Kirkdorffer <dankirkd_at_comcast.net>
Date: Mon, 19 Aug 2013 19:10:34 +0000 (UTC)

I'm seeing some oddities with the Spring Integration added with Jersey 2.2.

But firstly a question: does Jersey 2.2 only support Spring Dependency Injection with annotations? I can't get things to work at all with XML configurations.

But on to my observations with the Spring Integration (using annotations).

I would have expected that if I annotated my service with @Service I would get a singleton on each web service call via Jersey. But instead I see a new instance of my service is instantiated every time, albeit injected with the same instance of the one DAO I'm injecting into it. The same behavior is true if I user @Controller, or @Repository. However, if I use @Component the same instance of my service is returned each time.

I would have expected that all four of these annotations would have yielded the same results.

The other thing I find weird is that @Singleton behaves differently when used with these annotations. If I use @Singleton with @Service, @Controller, or @Repository, my constructor and setters are not called again after the first web service call. However, with @Component the setters are.

Finally, I was under the impression that @Scope("singleton") and @Singleton did the same thing (and of course that @Scope("singleton") was the same as not having the scope defined as "singleton" is the default). Well it seems not: if I use @Scope("singleton") instead of @Singleton, then for @Service, @Controller, or @Repository my constructor and setters are called, and I DON'T get the same instance of my service back each call, I get a different one. With @Component, the setters are called and I DO get the same instance of my service back each time.

So I've settled on using @Service @Singleton, but I'm scratching my head about why I'm seeing all this different behavior.

Dan



---
Daniel Kirkdorffer
dankirkd_at_comcast.net