mentby.com
Blog | Jobs | Help | Signup | Login
loading

JAX-RS and CDI integration using Glassfish v3

Last updated: Thu, 21 Jan 2010 07:38:32 -0800 View thread View thread at GlassFish Users

There are two ways CDI managed beans are enabled:

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

2) instantiated and managed by CDI. Annotate with a CDI scope  
annotation, like @RequestScoped (no @ManagedBean is
      required)

The reason for requiring an annotation is CDI can change the contract.  
For example section 3.7.1 of the 299 spec states:

If a bean class does not explicitly declare a constructor using  
@Inject, the constructor that accepts no parameters is the
bean constructor.

and section 5.1.1 of the 299 spec states:

5.5.1. Injection using the bean constructor
When the container instantiates a managed bean or session bean with a  
constructor annotated @Inject, the container calls
this constructor, passing an injectable reference to each parameter.  
If there is no constructor annotated @Inject, the con-
tainer calls the constructor with no parameters.

So if you have a root resource class like the following:

   @Path("foo")
   public class MyResource {
     public MyResource(@QueryParam("x") String x) { ... }
   }

then that class would fail if CDI was enabled by default. Other cases  
are related to client proxies.

Note that the JAX-RS specification does not require that JAX-RS based  
artifacts be bound such that 330's @Inject can be utilized. Jersey's  
integration does not currently support it, so the following will not  
work:

   @Path("foo")
   public class MyResource {
     @Inject
     public MyResource(@QueryParam("x") String x) { ... }
   }

it is something we need to implement (and may be non-trivial).

Paul.





Post a Comment