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:
[LIST] [*]@PersistenceUnit(unitName="SOME_NAME") [*]@Resource(name="JNDI_NAME") [*]@WebServiceRefs(lookup="JNDI_NAME_OF_WEB_SERVICE_REF")[/LIST]The main proposition of CDI is type-safety. This [B]T[/B]ip [B]O[/B]f [B]T[/B]he [B]D[/B]ay 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.
[LIST=1] [*]Create a Singleton-scoped bean or Application-scoped bean as:
import javax.inject.Singleton;@Singletonpublic 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:[LIST=1] [*]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; [/LIST][/LIST]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 [URL="http://fixunix.com/docs.jboss.org/weld/reference/1.0.0/en-US/html/"]latest documentation on Weld[/URL] (Reference Implementation for CDI and included in [URL="http://glassfish.org"]GlassFish[/URL]) for more details.