users@jersey.java.net

[Jersey] Re: Dependency Injection

From: Pavel Bucek <pavel.bucek_at_oracle.com>
Date: Mon, 17 Dec 2012 16:44:45 +0100

You can register your own ResourceConfig and add it there, similarly to
what the test does.

registering own ResourceConfig/javax.ws.rs.core.Application descendant
can be done via servlet/filter property:

         <init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.yourapp.YourConfig</param-value>
         </init-param>

or its even easier if you are using Servet 3.x deployment
(@ApplicationPath annotated javax.ws.rs.core.Application descendant) -
just add it to getSingletons() result.

I've tried to register my own provider as a class, but it does not work
for some reason - you can file a new issue if you want to have this
supported, but it is easily workaroundable by registering an instance
(you should have everything needed for its instantiation wheb
Application.getInstances() is called).

Regards,
Pavel

On 12/17/12 2:22 PM, Tomek Sniadach wrote:
> Thanks Pavel,
> this makes it a little bit clearer to me.
> In the test is the provider intatiated directly. I'm using tomcat.
> Where is here the place to configure the provider?
>
> Thanks and regards
> Tomek
>
>
>
> 2012/12/17 Pavel Bucek <pavel.bucek_at_oracle.com
> <mailto:pavel.bucek_at_oracle.com>>
>
> Hi Tomek,
>
> have you looked at AnnotationInjectableTest in Jersey workspace?
> It basically does exactly what you want, see
> http://java.net/projects/jersey/sources/svn/content/trunk/jersey/jersey-tests/src/test/java/com/sun/jersey/impl/inject/AnnotationInjectableTest.java?rev=5822
>
> Regards,
> Pavel
>
>
> On 12/17/12 10:58 AM, Tomek Sniadach wrote:
>
> Hi,
> I have trouble to understand, how dependency injection in
> jersey works. Let say, i have a class "Info" that should be
> instantiated at the boot time (tomcat). Then i need the
> reference to the class in my Resource.
> What I've done:
> I have a class
> public class Info implements InjectableProvider<Resource,
> Type> (see
> http://jersey.576304.n2.nabble.com/Simplest-Dependency-Injection-with-Jersey-td3393338.html)
> ..
> This is registered as provider (I can see the log message at
> boot time). Then I've got an error:
> java.lang.IllegalArgumentException: Can not set ...Info field
> ...MyResource.a to org.apache.naming.NamingContext
> In MyResource I have just a Field: Info a; annotated with
> @Resource
>
> Can someone explain the mechanism in jersey or post a link,
> where i can find more information?
>
> regards
>
> Tomek
>
>
>
>
>
>