Contexts & Dependency Injection (CDI) in Java EE 6 provides type-safe dependency injection. The type-safety part comes from the fact that no String-based identifiers are used for dependency injection. Instead CDI runtime uses the typing information that is already available in the Java object model.
Java EE 5 already had resource injection available in terms of PersistenceContext, PersistenceUnit, Resource, and others. But they require String-based identifiers to identify the resource to be injected. For example:
- @PersistenceUnit(unitName="SOME_NAME")
- @Resource(name="JNDI_NAME")
- @WebServiceRefs(lookup="JNDI_NAME_OF_WEB_SERVICE_REF")
The main proposition of CDI is type-safety. This Tip Of The Day explains how @Produces annotation provided by CDI can be used to centralize all these String-based resource injection and add a facade of type-safety on them. Specifically, it shows how type-safety can be achieved for @PersistenceUnit. A similar approach can be taken for other String-based resource injections as well.
- Create a Singleton-scoped bean or Application-scoped bean as:
import javax.inject.Singleton; @Singleton public class ApplicationResources { }
All the Java EE component environment references can be centralized in this bean.
- If the PersistenceUnit is currently initialized as:
@PersistenceUnit(unitName="StatesPU") EntityManagerFactory statesEMF;
in other Java EE components, such as Servlet, then it can be alternatively defined in the type-safe manner using the following steps:
- Define a new Qualifier as:
import static java.lang.annotation.ElementType.TYPE; import static java.lang.annotation.ElementType.FIELD; import static java.lang.annotation.ElementType.PARAMETER; import static java.lang.annotation.ElementType.METHOD; import static java.lang.annotation.RetentionPolicy.RUNTIME; import java.lang.annotation.Retention; import java.lang.annotation.Target; import javax.inject.Qualifier; @Qualifier @Retention(RUNTIME) @Target({METHOD, FIELD, PARAMETER, TYPE}) public @interface StatesDatabase { }
- Add the type-safe definition of "EntityManagerFactory" in "ApplicationResources" bean (defined above) as:
@Produces @PersistenceUnit(unitName="StatesPU") @StatesDatabase EntityManagerFactory statesEMF;
- The "EntityManagerFactory" can now be injected in the Servlet in a type-safe manner as:
@Inject @StatesDatabase EntityManagerFactory emf;
- Define a new Qualifier as:
This procedure can be repeated for other String-based resources as well and thus centralize all of them at one place. And now your application becomes more type-safe! With this TOTD, you can use @Inject for injecting your container- and application-managed resources easily.
Read the latest documentation on Weld (Reference Implementation for CDI and included in GlassFish) for more details.
Technorati: totd cdi javaee6 glassfish weld produces typesafety