users@jersey.java.net

Re: [Jersey] Inject CDI/Web Bean into filter

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Tue, 13 Apr 2010 10:11:24 +0200

On Apr 12, 2010, at 11:30 PM, John Lister wrote:

> Hi Paul, thanks for your answer...
>
> Ooops, you are indeed right that this does all work with a
> "standard" filter. I'd confused the issue as I was generating my
> filter from a factory which doesn't do the injection. Also I'd
> forgotten to make it managed by suitable annotation (I'd used
> @ManagedBean rather than @ApplicationScoped).


Did you make any progress, i am not sure you did based on your
previous email that i just replied to.


> I'm also using straight field injection - Is there any benefit/cost
> with the different forms of injection btw?
>

As i said constructor injection in CDI is a little bit awkward,
especially if final fields are utilized, as a default constructor is
required in addition to the @Inject annotated constructor [*]. Plus it
is not currently possible to intermix Jersey-based and CDI-based
parameters in a constructor (this is an integration issue that is
tricky to solve).

For CDI I recommend using field injection with @PostConstruct for post
processing or method injection:

   @Inject
   public void (Dep1 d1, Dep2 d2) { ... }



> Thanks again
>
> John
>
> ps. How is fatherhood going? Getting a bit of sleep I hope, I've got
> twin girls a couple of months older so forgive the occasional sleep
> deprived madness:)
>

Wow, congrats.

We were in survival mode for 3 days with myself and the first daughter
very sick, not fun. Better now. Sleeping is not bad. Lack of sleep
slows me down and i start forgetting things :-)

Paul.

[*] Guice best practices recommends using constructor injection as it
makes it easy to test your components.


>
> Paul Sandoz wrote:
>> It should be possible.
>>
>> CDI automatically proxies (see later) injected references. So as
>> long as your resource filter is a CDI managed singleton (annotate
>> with the CDI-based @ApplicationScoped) and you access the proxied
>> reference when a session is in scope it should work.
>>
>> Note that full constructor injection for CDI and Jersey is not
>> currently supported, meaning that it is not possible to inject
>> Jersey/JAX-RS specific parameters and CDI specific parameters in
>> the same constructor, only constructors with parameters consisting
>> of the latter will work if the bean is created by CDI. Getting
>> complete CDI constructor injection to work with Jersey is going to
>> require some fairly tricky work.
>>
>> Also note that using constructor injection with CDI actually looses
>> some of the benefits. Because CDI creates proxies you will also
>> need to add a default constructor (so the proxy instance can be
>> created) thus meaning that final fields cannot be used (which kind
>> of makes constructor injection less useful).
>>
>> If you share some code i might be able to help you further.
>>
>> Paul
>>
>> On Apr 8, 2010, at 9:55 PM, John Lister wrote:
>>
>>> Hi, is it possible to inject a CDI/Web bean into a resource
>>> filter, the reason I ask is that I am using a @SessionScoped bean
>>> to store data across requests and would like to access this in a
>>> filter.
>>>
>>> Originally I was injecting a HttpServletRequest into the
>>> constructor of a filter factory and passing this reference to the
>>> filters as I created them, which worked well. But I'd like to wrap
>>> the session stuff up using CDI which seems much neater and I'm
>>> struggling. Injecting the bean into the factory doesn't seem to
>>> work and as I create the filter I can't use injection there...
>>>
>>> Is this possible or am I going about it the wrong way? Suggestions
>>>
>>>
>>> Thanks
>>>
>>> John
>>>
>>> ---------------------------------------------------------------------
>>> 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
>