users@jersey.java.net

Re: [Jersey] Resource life cycle and injections

From: Erdinc Yilmazel <erdinc_at_yilmazel.com>
Date: Mon, 9 Mar 2009 18:29:54 +0000

Hi Paul,

I made tiny modifications in GuiceComponentProviderFactory. I did not
need my resource instances to be managed by guice, I think jersey
handles them perfectly. What I needed was being able to inject other
service instances into my Root resources using guice. So unless I
explicitly bind a resource interface to a resource class using a Guice
module, it is managed by jersey, otherwise it is managed by guice.
The getComponentProvider method in my implementation returns null
unless there is a binding in the injector for the Class that was sent
as an argument to the method.

In the original implementation there is a try catch block in the
getComponentProvider method. In this block it returns a
GuiceInstantiatedComponentProvider for every class that does not have
a binding in guice. I did not want this behavior so I replaced the
try-catch block with the following:


      if (!injector.getBindings().containsKey(Key.get(c))) {
         return null;
      }

I also did not need iterating all bound classes of the injector to
check if they are Resources or Providers. (In the register method).
This is because all my resource and provider instances are already
registered in the resource config.

I am sending my version of GuiceComponentProviderFactory in the attached file.

Thanks,

Erdinc


2009/3/9 Paul Sandoz <Paul.Sandoz_at_sun.com>:
>
> On Mar 9, 2009, at 11:01 AM, Erdinc Yilmazel wrote:
>
>> I managed to integrade Guice by looking at the jersey-guice module
>> source code in the contribs directory of jersey source.
>
> Great!
>
>
>> I am now
>> letting jersey manage my resource instances but I am able to inject
>> other services into them using guice.
>
> Did you make any modifications to the Guice code in the source? this is
> still "young" code so i would be interested to know what worked and if you
> needed to make modifications.
>
>
>> If I inject a ResourceContext
>> instance using @Context annotation, I can use its getResource  method
>> to access resource instances.
>>
>> How can I get an instance of javax.ws.rs.core.Application ?
>>
>
> It is possible to get access to the Jersey specific configuration:
>
>  @Context ResourceConfig rc;
>
> which extends Application.
>
> If you declare a specific implementation of Application that does not extend
> from ResourceConfig, then Jersey will wrap this around an instance of
> ApplicationAdapter that extends ResourceConfig.
>
> Please log an issue if you require the instance of the Application class (if
> declared) to be injected.
>
> Paul.
>
>> Erdinc
>>
>> On Mon, Mar 9, 2009 at 1:26 AM, Jaka Jančar <jaka_at_kubje.org> wrote:
>>>
>>> JAX-RS does not define @Inject. There's a good intro to Guice on YouTube.
>>>
>>> Resources are created per-request unless annotated using @Singleton.
>>>
>>> You can access those instances using the getSingletons() method from
>>> javax.ws.rs.core.Application.
>>>
>>> Jaka
>>>
>>>> Erdinc Yilmazel wrote:
>>>>>
>>>>> Hello,
>>>>>
>>>>> Where can I find detailed information about life cycle of resource
>>>>> instances and how to use the @Inject annotation in resource classes? I
>>>>> know that if I use @Singleton annotation jersey creates just one
>>>>> instance for the resource, however I don't know how I can access this
>>>>> instance that jersey created for me.
>>>>>
>>>>> I want to use a dependency injection framework such as guice to manage
>>>>> the resource instances and injections that are made in these objects.
>>>>> Can you show me a starting point in implementing such behavior?
>>>>>
>>>>> Thank you,
>>>>> Erdinc
>>>
>>>
>>> ---------------------------------------------------------------------
>>> 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
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>
>