Search This Blog

Loading...

Friday, March 2, 2012

JPA: Injection level-types in JavaEE5+

There are 3 types of resource injections in JavaEE in the order of usage frequency:
  1. Field injection.
  2. Setter injection.
  3. Type-level injection. 
Unlike field and setter injections, type-level injections are not really injection. It does not inject anything into anywhere; it just declares a resource reference in a descriptor-free manner.

To use resource injected at type-level, you will need to do a traditional JNDI lookup. What we have saved here, compared to pre-JavaEE-5, is there is no need for deployment descriptors.

A servlet example in glassfish:


Type-level injections are inherently verbose, compared to field and setter injections, where a bare @EJB or @Resource is sufficient in some cases. Field and setter injections can use the field type and parameter type to infer the resource type, respectively.

Type-level injections can't. All the necessary attributes must be present.

  • For @Resource, you must specify at least name and type
  • For @EJB, you must have at least beanInterface and name.