users@jersey.java.net

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

From: John Lister <john.lister_at_kickstone.com>
Date: Mon, 12 Apr 2010 22:30:48 +0100

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). I'm also using straight field injection - Is there
any benefit/cost with the different forms of injection btw?

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:)


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
>>