users@jersey.java.net

Re: [Jersey] Jersey and Java EE 6 - Context and Dependency Injection

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Thu, 21 Jan 2010 12:28:20 +0000

On Jan 21, 2010, at 10:59 AM, cmoulliard wrote:

>
> Hi,
>
> I have a simple question : Is Jersey injection compatible with CDI
> of java
> EE 6 specification ?
>

Yes, but there are restrictions, thus it is not possible in all cases
to switch from non-CDI to CDI-instantiated/managed without code changes.


> Can we use @Inject annotation of Jersey in a Java EE 6 Web Application
> Server ?

You can, but it does not have the same semantics as the 330 @Inject.
However, there are certain restrictions.

If the resource or provider class is instantiated by CDI then it is
not currently possible to support injection of Jersey/JAX-RS stuff on
constructor parameters.

This is a limitation in Jersey's integration with Weld (the CDI
implementation) but also such constructor injection is not required by
the JAX-RS spec. Constructor injection is the hardest thing to
integrate because there is no "carte blanche" like one has with say a
post construct listener and you have to integrate closely with the
binding mechanism.


> Can we combine CDI annotations (like @managed-bean) with Jersey REST
> annotations ?
>

There are two mechanisms:

1) instantiated by CDI, life-cycle managed by Jersey. Annotate with
@ManagedBean and annotate with a Jersey scope
      annotation.

2) instantiated and managed by CDI. Annotate with a CDI scope
annotation, like @RequestScoped.

Paul.