Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[jaxrs-dev] CDI resource classes and lifecycle

Hi all,

I've a question regarding CDI and the lifecycle of resource classes.

The JAX-RS spec states:
  • 3.1.1 Lifecycle and Environment:
    • "By default a new resource class instance is created for each request to that resource."
  • 11.2.3 Context and Dependency Injection (CDI):
    • "In a product that supports CDI, implementations MUST support the use of CDI-style Beans as root resource classes"

The example CDI resource in section 11.2.3 looks like this:

  @Path("/cdibean")
  public class CdiBeanResource {

      @Inject MyOtherCdiBean bean;  // CDI injected bean

      // ...

   }

Please note that the resources doesn't have a scope annotation. In this case CDI will assume @Dependent scope.

Isn't this weird? Shouldn't be the default scope for CDI resources be @RequestScoped? Is it up to the user to set the "correct" scope? It is somehow surprising that @Dependent scoped beans work at all because of the way @Dependent is defined.

Any thoughts? Or is it just me having doubts about this? :-)

Christian

--

Back to the top