users@jersey.java.net

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

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Mon, 12 Apr 2010 10:46:25 +0200

Hi,

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
>